Class1:
Class2:
Class3:
Class4:
Class2:
Class3:
Class4:
There are many changes in Servlet 3.0 Specification, web.xml file is optional, servlets, listeners and filters are no longer required to be defined in web.xml, instead they can declared using annotations. web.xml file can be devided into multiple files called webfragments.Simple Servlet example
Note Directory and file names are case sensitive.
javac WelcomeServlet.java It will create the file WelcomeServlet.class in the same directory. Copy the class file to classes directory. All the Servlets and other classes used in a web application must be kept under WEB-INF/classes directory. Note: to compile a servlet you need to have servlet-api.jar file in the class path.The deployment descriptor (web.xml) file.
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>WelcomeServlet</servlet-name> <servlet-class>jsptube.tutorials.servletexample.WelcomeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>WelcomeServlet</servlet-name> <url-pattern>/WelcomeServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file> /pages/form.html </welcome-file> </welcome-file-list> </web-app>
CLASSPATH.WLHOME/ext directory, and edit the script WLHOME/config/mydomain/startWebLogic.cmd"./ext/postgresql.jar" to the CLASSPATH setting there. - Configure a new JDBC Connection Pool
- General tab:
- Name: Any name you like for your pool (eg MyPoolName)
- URL: jdbc:postgresql://hostname:port/database
eg jdbc:postgresql://smirnoff.uk.ingenotech.com:5432/homepages
- Driver Classname: org.postgresql.Driver
- Properties: user=ed password=anything
- ACL Name: (blank)
- Password: The real password to connect to the database, this
will be kept encrypted and substituted for the value
you put in "Properties".
- Press "Create"
- Go to the "Targets" tab
- Select "myserver" from the "Abailable" list and move it
to "Chosen". Apply.
- Create a JDBC Data Source which references the Connection Pool above
- Configure a new JDBC Data Source - Configuration Tab
- Name: Any name you like (eg MyDataSource)
- JNDI Name: Make this the same as "Name" above (MyDataSource).
- Pool Name: The name of the ConnectionPool created above (MyPoolName)
- Press "Create"
- Go to the "Targets" tab
- Select "myserver" from the "Available" list and move it
to "Chosen". Apply.
java -cp {path to weblogic.jar} weblogic.Admin -url localhost:7001 \
-username system -password password \
CREATE_POOL .... TBD ....
- Add the following to create a connection pool:
<JDBCConnectionPool DriverName="org.postgresql.Driver"
MaxCapacity="10"
Name="MyPoolName"
Password="{3DES}yIsebsUSRdE="
Properties="user=ed;password=secret"
Targets="myserver"
URL="jdbc:postgresql://hostname:port/database"/>
- Add the following to create a Data Source referencing that pool:
<JDBCDataSource JNDIName="MyDataSource"
Name="MyDataSource"
PoolName="MyPoolName"
Targets="myserver"/>
<resource-ref>
<res-ref-name>jdbc/MyPoolName</res-ref-name> <!-- This is the name chosen for the Connection Pool with "jdbc/" prepended -->
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<reference-descriptor>
<resource-description>
<res-ref-name>jdbc/MyPoolName</res-ref-name> <!-- This is the name chosen for the Connection Pool with "jdbc/" prepended -->
<jndi-name>MyDataSource</jndi-name> <!-- this is the name you chose for the DataSource -->
</resource-description>
</reference-descriptor>
private Connection getConnection() throws NamingException
{
InitialContext ic = new InitialContext();
DataSource ds = (DataSource)ic.lookup("java:comp/env/jdbc/MyPoolName");
return ds.getConnection();
}
Connection conn = null;
PreparedStatement st = null;
try
{
conn = getConnection();
// Prepare your SQL statement, substitute "somevalue" for the first "?" parameter.
st = conn.prepareStatement("SELECT COL1, COL2, COL3 FROM TABLENAME WHERE COL1 = ?");
st.setString(1, somevalue);
// Execute the SQL and read the rows returned
ResultSet rs = st.executeQuery();
while (rs.next())
{ // Read and process each row of the ResultSet
String col1 = rs.getString(1);
String col2 = rs.getString(2);
String col3 = rs.getString(3);
// etc...
}
}
catch (SQLException ex)
{
System.out.println("SQL exception occurred" +ex);
}
finally
{
try
{
if (st != null)
st.close();
}
catch (SQLException ex)
{
}
try
{
if (conn != null)
conn.close();
}
catch (SQLException ex)
{
}
}