0

I am trying to deploy a simple web application. I only have 2 files, Entry.html and UpdateDBServlet. I have placed them under a directory in the webapps of tomcat. I can access the entry.html when Tomcat is running, but upon pressing the submit button, the UpdateDBServlet should be called, however, I get a html 404 error indicating that the requested resource is unavailable. I dont know how else to place this resource. Should this servlet also be mapped in the web.xml file?

4
Contributors
11
Replies
12
Views
7 Years
Discussion Span
Last Post by raghujosh
0

Your server cannot find your Java class that should be inside PROEJCT_FOLDER/WEB-INF/classes. For more details on project folder structure check Tomcat documentation here

0

Your server cannot find your Java class that should be inside PROEJCT_FOLDER/WEB-INF/classes. For more details on project folder structure check Tomcat documentation here

I did precompile and keep the class file in the employee/web-inf/classes folder, however I still get the same error.

Here is my code of the HTML file

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional //EN">

<html>
<head>
	<title>Enter Employee Information</title>
</head>

<body>
<table cellspacing="5" border="0">
<tr>
	<td align="right">First Name:</td>
	<td><input type="text" name="firstName"></td>
</tr>
<tr>
	<td align="right">Last Name:</td>
	<td><input type="text" name="lastName"></td>
</tr>
<tr>
	<td align="right">Address:</td>
	<td><input type="text" name="address"></td>
</tr>
<tr>
	<td align="right">Designation:</td>
	<td><input type="text" name="designation"></td>
</tr>
<tr>
	<td align="right">Salary:</td>
	<td><input type="text" name="salary"></td>
</tr>
<tr>
	<td align="right">Work Phone:</td>
	<td><input type="text" name="workPhone"></td>
</tr>
<tr>
	<td align="right">Home Phone:</td>
	<td><input type="text" name="homePhone"></td>
</tr>

</table>
<form action = "UpdateDBServlet" method = "post">
<input type = "submit" value = "Submit">
</form>
</body>
</html>

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

0

1. Your HTML is wrong. Form tags should encapsulate fields you want to retrieve
2. If you post also Java class and web.xml file I may try to run it and see where is problem

0

1. Your HTML is wrong. Form tags should encapsulate fields you want to retrieve
2. If you post also Java class and web.xml file I may try to run it and see where is problem

My Servlet

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class UpdateDBServlet extends HttpServlet
{
public void doPost(
HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
//get parameters from request
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String address = request.getParameter("address");
String designation = request.getParameter("designation");
String salary = request.getParameter("salary");
String workPhone = request.getParameter("workPhone");
String homePhone = request.getParameter("homePhone");

//open a database connection

	String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	String url = "jdbc:sqlserver://RAGHU:1433;databaseName=Employee;integratedSecurity=true";
	String username = "Raghavendra";
	String password = "";


	Connection conn=null;
	Statement stmt=null;
	ResultSet rs=null;
	try
	{
		Class.forName(driver);
		conn = DriverManager.getConnection(url, username, password);
		stmt = conn.createStatement();
		String Query = "INSERT INTO Employee Values (firstName, lastName, address, designation, salary, workPhone, homePhone)";
		rs = stmt.executeQuery(Query);


	}
	catch(Exception e)
	{
		e.printStackTrace();
		System.err.println(e.getMessage());
	}

	response.setContentType("text/html");
	PrintWriter out =response.getWriter();
	out.println("<h1>HTML from Servlet</h1>");
	out.close();

}
public void doGet(
HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
	doPost(request, response);
}


}

My WEB.XML

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
  <servlet>
  	<servlet-name>UpdateDBServlet</servlet-name>
  	<servlet-class>UpdateDBServlet</servlet-class>
  </servlet>

</web-app>

Edited by raghujosh: Wrapping the code in the post. Was not aware of the rules earlier

0

Hi raghujosh, i go through you post and I think there is an error in web.xml file content. Here is mine code try it.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>UpdateDBServlet</servlet-name>
        <servlet-class>lak.UpdateDBServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UpdateDBServlet</servlet-name>
        <url-pattern>/UpdateDBServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

Please put your servlet in a Package, Here my package name is lak.

0

Hi raghujosh, i go through you post and I think there is an error in web.xml file content. Here is mine code try it.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>UpdateDBServlet</servlet-name>
        <servlet-class>lak.UpdateDBServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UpdateDBServlet</servlet-name>
        <url-pattern>/UpdateDBServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

Please put your servlet in a Package, Here my package name is lak.

I tried your code, but now i get a HTTP status 500 error. Here is the full error report.
The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Wrapper cannot find servlet class lak.UpdateDBServlet or a class it depends on
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861)
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584)
java.lang.Thread.run(Thread.java:619)


root cause

java.lang.ClassNotFoundException: lak.UpdateDBServlet
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1645)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1491)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861)
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584)
java.lang.Thread.run(Thread.java:619)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.28 logs.

Here is my directory structure
A)webapps
A1)employee
A1.1)Entry.html
A1.2)Web-Inf
A1.2.1)web.xml
A1.2.2)classes
A1.2.2.1)lak
A1.2.2.1.1) UpdateDBServlet.class

Edited by raghujosh: added more content

0

@raghujosh sorry for late reply. To solve 404 that you had originally you need to included mapping for your servlet. Just add mapping as showed bellow and you are sorted. You will need to go back to your original folder layout

<servlet>
  	<servlet-name>UpdateDBServlet</servlet-name>
  	<servlet-class>UpdateDBServlet</servlet-class>
  </servlet>
<servlet-mapping>
        <servlet-name>UpdateDBServlet</servlet-name>
        <url-pattern>/UpdateDBServlet</url-pattern>
    </servlet-mapping>

As for the second one, well I believe you compiled your Java file directly from the folder where is saved. Because your class has now package it will create new folder for you named by package and your class will be stored there and hence server cannot find it (not even manual copy&paste would help). Make sure that when you compile you are outside of you classes directory such as

javac -classpath /PATH_TO_TOMCAT/lib/servlet-api.jar:classes:. /classes/UpdateDBServlet.java

Incase of Windows OS replace servlet-api.jar:classes:. with servlet-api.jar;classes:.

0

@raghujosh sorry for late reply. To solve 404 that you had originally you need to included mapping for your servlet. Just add mapping as showed bellow and you are sorted. You will need to go back to your original folder layout

<servlet>
  	<servlet-name>UpdateDBServlet</servlet-name>
  	<servlet-class>UpdateDBServlet</servlet-class>
  </servlet>
<servlet-mapping>
        <servlet-name>UpdateDBServlet</servlet-name>
        <url-pattern>/UpdateDBServlet</url-pattern>
    </servlet-mapping>

As for the second one, well I believe you compiled your Java file directly from the folder where is saved. Because your class has now package it will create new folder for you named by package and your class will be stored there and hence server cannot find it (not even manual copy&paste would help). Make sure that when you compile you are outside of you classes directory such as

javac -classpath /PATH_TO_TOMCAT/lib/servlet-api.jar:classes:. /classes/UpdateDBServlet.java

Incase of Windows OS replace servlet-api.jar:classes:. with servlet-api.jar;classes:.

Hi Peter, thanks for the guidance. Can you pls suggest me a directory structure? There are only 2 files Entry.html, and UpdateDBServlet. This is the first time I am doing this and so keep on hitting roadblocks.

0

PROJECT_DIRECTORY

  • *.html, *.jsp
  • images
  • css
  • WEB-INF
    • web.xml
    • classes
      • *.java
      • packages
    • lib (any additional libraries you may need JDBC, JSTL, JSF, Hibernate, etc)
0

PROJECT_DIRECTORY

  • *.html, *.jsp
  • images
  • css
  • WEB-INF
    • web.xml
    • classes
      • *.java
      • packages
    • lib (any additional libraries you may need JDBC, JSTL, JSF, Hibernate, etc)

For some reason, I was unable to sort this matter. So I installed Eclipse Web Tools Platform(WTP) which takes care of a lot of configuration issues and this solved my problem.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.