Natela Natela - 2 months ago 16
Java Question

Error with Java Servlet & DB connection

Having issues getting the program to display anything when running in eclipse.

Goal is:
In this application you will also create a virtual directory so that you do not have to use the word servlet as part of your form post URL. You will then be able to run your Web application servlet by using a URL similar to the following: http://localhost:7070/Week5/FormPost2. To create your virtual directory you may start by modifying the web.xml attached to this assignment. Next, create a Servlet that displays a form when the doGet method is invoked. The form will contain a post action that directs the form post back to the same servlet, which in the doPost method will save the form data to a database. Use your Oracle account to make the DB connection. After the form data has been saved to the database, respond back with a query from the database displaying all the current records contained in the database, in an appealing format. The form must contain a minimum of three input fields.

Below error was received:

HTTP Status 404 - /Week5/servlet/Week5.Week5



type Status report
message /Week5/servlet/Week5.Week5

description The requested resource is not available.



Apache Tomcat/7.0.72

I'm sure it is a simple error, but please pinpoint it so I can get it to display properly. Thank you.

package Week5;

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

public class Week5 extends HttpServlet{
private static final long serialVersionUID = 1L;

Connection con = null;
Statement stmt = null;

public Week5(){
init();
}
public void init(){
try{
DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "student2", "pass");
stmt = con.createStatement();
stmt.executeUpdate("CREATE TABLE MYTABLE (FNAME VARCHAR2(20), LNAME VARCHAR2(40), PHONE VARCHAR2(20))");
stmt.close();
}
catch (Exception e){
}
}

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

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("First Name:");
out.println("");
out.println("");
out.println("Last Name:");
out.println("");
out.println("");
out.println("Phone:");
out.println("");
out.println("SUBMIT");
out.println("");
out.println("");
out.close();
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
try{
if (con != null)
init();
String fname = request.getParameter("FNAME");
String lname = request.getParameter("LNAME");
String phone = request.getParameter("PHONE");
Statement stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO MYTABLE VALUES('" + fname + "', '" + lname + "', '" + phone + "')");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
ResultSet rset = stmt.executeQuery("SELECT * FROM MYTABLE");
while (rset.next()){
out.print("");
out.print("First Name: " + rset.getString(1));
out.print("");
out.println();
out.print("");
out.print("Last Name: " + rset.getString(2));
out.print("");
out.println();
out.print("");
out.print("Phone: " + rset.getString(3));
out.print("");
out.println();
out.println();
}
out.println("");
out.close();
stmt.close();
}

catch (Exception e){

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println(e.getMessage());
out.println("");
out.close();
}
}
}


XML


<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">

<web-app>

<servlet>
<servlet-name>
Week5 <!-- Alias I gave the servlet // -->
</servlet-name>
<servlet-class>
HelloWorld <!-- Class name // -->
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>
Week5 <!-- Alias I gave the servlet // -->
</servlet-name>
<url-pattern>
/VirtualName <!-- What I want the user to type in // -->
<!-- Example: http://localhost:7070/Week5/FormPost2 // -->
</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>
FormPost2
</servlet-name>
<servlet-class>
HelloWorld2
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>
FormPost2
</servlet-name>
<url-pattern>
/VirtualName2
</url-pattern>
</servlet-mapping>

</web-app>

Answer

your servlet class should be fully qualified class name such as

<servlet-class>
week5.week5
</servlet-class>

<servlet-class> tag should contain the servlet(class) <servlet-name> will be the name of the servlet that is mapped with the <servlet-mapping>

example

<servlet>
<servlet-name>
anyServletName
</servlet-name>
<servlet-class>
com.example.customServlet
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>
anyServletName
</servlet-name>
<url-pattern>
/VirtualName2
</url-pattern>
</servlet-mapping>
Comments