Hi,
i am having trouble running my servlet on tomcat, baisically i get this error in my catalina.out log and cannot figure what went wrong;
---The servlet code i'm using is as follows ;
Which basically creates a database connection object which opens a database connection in its constructor and starts up a jsp page so the user can login. Maybe the jsp call the servlets init method again before if calls the post. Any help is appreciated.
--I thought there might be another process using this port so I ran netstat -a|grep
--20000(the port I’m running tomcat on
prompt> netstat -a|grep 20000
*.20000 *.* 0 0 49152 0 LISTEN
-- this is what i get when i load the servlet:
prompt> netstat -a|grep 20000
*.20000 *.* 0 0 49152 0 LISTEN
machine.20000 machine-address 33580 0 49640 0 TIME_WAIT
-- this is what i get just after hitting the submit button:
prompt> netstat -a|grep 20000
*.20000 *.* 0 0 49152 0 LISTEN
machine.20000 machine-address 33580 0 49640 0 TIME_WAIT
machine.20000 machine-address 33580 0 49640 0 TIME_WAIT
--The jsp page and DBConn code is as follows;
i am having trouble running my servlet on tomcat, baisically i get this error in my catalina.out log and cannot figure what went wrong;
Code:
Stopping service Tomcat-Standalone
Stopping service Tomcat-Apache
ption: Address already in use
LifecycleException: null.open: java.net.BindException: Address already in use
at org.apache.catalina.connector.http.HttpConnector.initialize(Unknown Source)
at org.apache.catalina.core.StandardService.initialize(Unknown Source)
at org.apache.catalina.core.StandardServer.initialize(Unknown Source)
at org.apache.catalina.startup.Catalina.start(Unknown Source)
at org.apache.catalina.startup.Catalina.execute(Unknown Source)
at org.apache.catalina.startup.Catalina.process(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.catalina.startup.Bootstrap.main(Unknown Source)
----- Root Cause -----
java.net.BindException: Address already in use
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:331)
at java.net.ServerSocket.bind(ServerSocket.java:309)
at java.net.ServerSocket.<init>(ServerSocket.java:183)
at java.net.ServerSocket.<init>(ServerSocket.java:139)
at org.apache.catalina.net.DefaultServerSocketFactory.createSocket(Unknown Source)
at org.apache.catalina.connector.http.HttpConnector.open(Unknown Source)
at org.apache.catalina.connector.http.HttpConnector.initialize(Unknown Source)
at org.apache.catalina.core.StandardService.initialize(Unknown Source)
at org.apache.catalina.core.StandardServer.initialize(Unknown Source)
at org.apache.catalina.startup.Catalina.start(Unknown Source)
at org.apache.catalina.startup.Catalina.execute(Unknown Source)
at org.apache.catalina.startup.Catalina.process(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.catalina.startup.Bootstrap.main(Unknown Source)
StandardServer.await: create: java.net.BindException: Address already in use
java.net.BindException: Address already in use
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:331)
at java.net.ServerSocket.bind(ServerSocket.java:309)
at java.net.ServerSocket.<init>(ServerSocket.java:183)
at org.apache.catalina.core.StandardServer.await(Unknown Source)
at org.apache.catalina.startup.Catalina.start(Unknown Source)
at org.apache.catalina.startup.Catalina.execute(Unknown Source)
at org.apache.catalina.startup.Catalina.process(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.catalina.startup.Bootstrap.main(Unknown Source)
Starting service Tomcat-Standalone
Apache Tomcat/4.0
Starting service Tomcat-Apache
Apache Tomcat/4.0
---The servlet code i'm using is as follows ;
Code:
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*@version SurfProjectLogin.java 1.0 04/02/22
*@author james
*/
public class LoginServlet extends HttpServlet {
private DBConn Dbc;
/**
*init method initialises Dbc class variable and sets up a database connection
*/
public void init(){
Dbc = new DBConn();
}
/**
*service method tries to show the Login page on startup
*/
public void service( HttpServletRequest req, HttpServletResponse res) throws ServletException, java.io.IOException{
gotoPage("/Login.jsp",req, res);
}
/**
*doGet method
*/
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException, java.io.IOException {
System.out.println("hello from doget()");
}
/**
*doPost method
*/
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException, java.io.IOException {
boolean isValidUser=false;
java.sql.ResultSet rs;
String password=req.getParameter("pwd");
rs = Dbc.query("select Password from SurfEmployees");
try{
while (rs.next()) {
String s = rs.getString("Password");
if (password.equals(s))
isValidUser=true;
}
if (isValidUser == true)
gotoPage("/Results.jsp",req,res);
else
gotoPage("/Error.jsp",req,res);
}catch(java.sql.SQLException sqle){
getServletContext().log("ERROR WHILE SEARCHING THRO THE RESULT SET");
}
}
/**
*sends the user to a specified page
*@param address where the page is
*@param req Request obj
*@param res Response obj
*/
private void gotoPage(String address, HttpServletRequest req,HttpServletResponse res) throws ServletException, java.io.IOException{
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
dispatcher.forward(req, res);
}
/**
*destroy method closes the database connection
*/
public void destroy(){
Dbc.closeDBConn();
Dbc=null;
}
}
Which basically creates a database connection object which opens a database connection in its constructor and starts up a jsp page so the user can login. Maybe the jsp call the servlets init method again before if calls the post. Any help is appreciated.
--I thought there might be another process using this port so I ran netstat -a|grep
--20000(the port I’m running tomcat on
prompt> netstat -a|grep 20000
*.20000 *.* 0 0 49152 0 LISTEN
-- this is what i get when i load the servlet:
prompt> netstat -a|grep 20000
*.20000 *.* 0 0 49152 0 LISTEN
machine.20000 machine-address 33580 0 49640 0 TIME_WAIT
-- this is what i get just after hitting the submit button:
prompt> netstat -a|grep 20000
*.20000 *.* 0 0 49152 0 LISTEN
machine.20000 machine-address 33580 0 49640 0 TIME_WAIT
machine.20000 machine-address 33580 0 49640 0 TIME_WAIT
--The jsp page and DBConn code is as follows;
Code:
<HTML>
<HEAD>
<TITLE>Welcome to the login page</TITLE>
</HEAD>
<BODY>
<h1>Please Enter Your Login Details</h1>
<form name="login" action="LoginServlet" method="post">
Name: <input type="text" name="name"><br>
Password:<input type="password" name="pwd"><br>
<input type="submit" name="Login" value="Login"><br>
<input type="hidden" name="action" value="LOGIN">
</form>
</BODY>
</HTML>
import java.sql.*;
public class DBConn{
//data members
private Connection conn;
private Statement stmt;
/**
*Overrides the default constructor
*/
public DBConn(){
this.openDBConn();
}
/**
*Method attempts to connect to SOME DATABASE
*/
protected void openDBConn(){
try{
//Load mysql driver
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//make a connection
String url = "jdbc:mysql://localhost/mydb";
conn = DriverManager.getConnection(url);
//Create and instantiate a statement obj
stmt = conn.createStatement();
}catch(Exception e){
System.out.println("Error opening connection.");
e.printStackTrace();
}
}
/**
*Method that queries the Database and returns a result set
*@param q String containg lookup details
*@return rs Result Set containing the queried result
*/
protected ResultSet query(String q){
ResultSet rs=null;
try{
rs = stmt.executeQuery(q);
}catch(SQLException sqle){
System.out.println("Error querying the database.");
sqle.printStackTrace();
}
return rs;
}
/**
*Method attempts to close database connection.
*/
protected void closeDBConn(){
try{
stmt.close();
conn.close();
}catch(Exception e){
System.out.println("Error closing connection.");
e.printStackTrace();
}
}
}