• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet is not working

 
seraj ela
Greenhorn
Posts: 22
Android MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends m new to servlet programming. am using eclipse IDE
i followed a tutorial to fetch data from mysql. nothing error is showing.
It shows like server is working fine.But result is not displayed. Please help me out.

servlet coding
public class Databaseaccess extends HttpServlet {
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
//JDBC driver name and database URL
final String DB_URL="jdbc:mysql://localhost/dblogin";
java.sql.Connection conn = null;
Statement stmt = null;
// Database credentials
final String USER = "root";
final String PASS = "root";

//Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Database Result";
String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " +"transitional//en\">\n";
out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" + "<h1 align=\"center\">" + title + "</h1>\n");

try{
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
conn = DriverManager.getConnection(DB_URL,USER,PASS);

// Execute SQL query
stmt = conn.createStatement();
String sql;
sql = "SELECT username,name FROM usertable";
ResultSet rs = stmt.executeQuery(sql);

// Extract data from result set
while(rs.next()){
//Retrieve by column name
String uname = rs.getString("username");
String name = rs.getString("name");

//Display values
out.println(", First: " + uname + "<br>");
out.println(", Last: " + name + "<br>");
}
out.println("</body></html>");

// Clean-up environment
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
} //end try
}
}

 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi elakiya ,

Welcome...

How are you trying to run servlet, i mean is it accessible when you do some action.First check is your servlet accessible or not by setting debugger in the servlet class file.

Later issue can be figured out if database is accessible or not.

Thanks
 
Tim Moores
Bartender
Posts: 2839
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
elakiya selvi wrote:But result is not displayed.

What does this mean? What are you doing, and what is the result? Which parts of the code are or are not executed?
 
seraj ela
Greenhorn
Posts: 22
Android MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am new to servlet and m working on my college project.
i think servlet is not working.

Status report

message /Databaseaccess/

description The requested resource (/Databaseaccess/) is not available.


Server console


Apr 28, 2012 8:38:56 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre6\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre6/bin/client;C:/Program Files/Java/jre6/bin;C:/Program Files/Java/jre6/lib/i386;C:\PROGRAM FILES\PC CONNECTIVITY SOLUTION\;C:\Windows\SYSTEM32;C:\Windows;C:\Windows\SYSTEM32\WBEM;C:\Windows\SYSTEM32\WINDOWSPOWERSHELL\V1.0\;C:\PROGRAM FILES\WIDCOMM\BLUETOOTH SOFTWARE\;C:\PROGRAM FILES\DELL\DW WLAN CARD;C:\MAVEN\APACHE-MAVEN-3.0.2\BIN;C:\SPRING-ROO-1.1.0.M1\BIN;;C:\PROGRAM FILES\PANDA SECURITY\PANDA GLOBAL PROTECTION 2011;C:\WINDOWS\SYSTEM32\GS\GS7.05\BIN;C:\PROGRAM FILES\MYSQL\MYSQL SERVER 5.5\BIN;C:\Program Files\Microsoft Visual Studio\Common\Tools\WinNT;C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin;C:\Program Files\Microsoft Visual Studio\Common\Tools;C:\Program Files\Microsoft Visual Studio\VC98\bin;C:\Program Files\eclipse-java-indigo\eclipse;;.
Apr 28, 2012 8:38:57 PM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Apr 28, 2012 8:38:57 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1403 ms
Apr 28, 2012 8:38:57 PM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Apr 28, 2012 8:38:57 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.35
Apr 28, 2012 8:38:57 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor Databaseaccess.xml
Apr 28, 2012 8:38:57 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor Servlettest.xml
Apr 28, 2012 8:38:57 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Apr 28, 2012 8:38:57 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Apr 28, 2012 8:38:58 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/60 config=null
Apr 28, 2012 8:38:58 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 867 ms
 
Tim Moores
Bartender
Posts: 2839
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet is never accessed because apparently nothing is mapped to the URL you're trying to access. How are you mapping the servlet in the web.xml file?
 
seraj ela
Greenhorn
Posts: 22
Android MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Databaseaccess</display-name>
<servlet>
<description></description>
<display-name>Databaseaccess</display-name>
<servlet-name>Databaseaccess</servlet-name>
<servlet-class>Databaseaccess</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Databaseaccess</servlet-name>
<url-pattern>/Databaseaccess</url-pattern>
</servlet-mapping>
</web-app>
 
Prasad Krishnegowda
Ranch Hand
Posts: 670
4
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, put your servlet in package, use of default package is not encouraged.
From Java 1.4, classes in default package cannot be imported.
 
Tim Moores
Bartender
Posts: 2839
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which URL are trying to access? It should be something like http://localhost:8080/name_of_your_web_app/Databaseaccess.

And yes, get into the habit of keeping your classes in packages.
 
seraj ela
Greenhorn
Posts: 22
Android MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am access this link
http://localhost:8080/Databaseaccess/
 
seraj ela
Greenhorn
Posts: 22
Android MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya put it in package.

<servlet-class>com.ela.database.Databaseaccess</servlet-class>
 
Prasad Krishnegowda
Ranch Hand
Posts: 670
4
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim, has already told you how to invoke your servlet, did you get that? it should be http://localhost:8080/<yourwebappname>/Databaseaccess..

 
seraj ela
Greenhorn
Posts: 22
Android MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didnt get what is mean by< web app name>.am sorry.please help me out
 
Tim Moores
Bartender
Posts: 2839
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
elakiya selvi wrote:http://localhost:8080/Databaseaccess/

This would only be correct if the servlet is in the ROOT web app, which most likely it is not. The name of the web app is the name of the directory underneath Tomcat's webapps directory in which your code lives.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64958
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct name for this is context path.
 
seraj ela
Greenhorn
Posts: 22
Android MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am using eclipse to develop program..
my servlets resides in <wtpwebapps> along with ROOT.(D:\mkm\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps)
and accessed with the link
http://localhost:8080/wtpwebapps/Databaseaccess/

still the resources is not found.
 
Tim Moores
Bartender
Posts: 2839
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leave out the trailing slash in the URL.
 
seraj ela
Greenhorn
Posts: 22
Android MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still resource is unavailable.

following warning is showed in console.

INFO: Deploying configuration descriptor D:\mkm\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\Databaseaccess.xml
Apr 29, 2012 7:53:26 AM org.apache.catalina.startup.SetContextPropertiesRule begin
WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.serveratabaseaccess' did not find a matching property.
Apr 29, 2012 7:53:27 AM org.apache.catalina.startup.HostConfig deployDescriptor
 
Runrioter Wung
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you solve the problem,now? I think that the String named "DB_URL" is wrong. where is the number of port? The default number is 3306.
For example:
 
seraj ela
Greenhorn
Posts: 22
Android MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runrioter Wung ya got worked now...thank you so much
 
seraj ela
Greenhorn
Posts: 22
Android MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
friends thank you so much for helping me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic