I am writing a simple servlet program in which I am trying to save web form data(jsp) to mySQL database. I am getting a http status 404. Here is my code
My FirstServlet.java file is
package first;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
public class FirstServlet extends HttpServlet { private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public FirstServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Employee Management form";
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=\"#f0f0c0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<ul>\n" +
" <li><b>First Name</b>: "
+ request.getParameter("first_name") + "\n" +
" <li><b>Last Name</b>: "
+ request.getParameter("last_name") + "\n" +
"</ul>\n" +
"</body></html>");
try {
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/world", "root" , "password");
System.out.println("Loading driver...");
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded!");
Statement myStmt = myConn.createStatement();
//ResultSet myRs = myStmt.executeQuery("select * from countrylanguage");
String sql = "insert into countrylanguage"
+ " (CountryCode, Language, IsOfficial, Percentage)"
+ " VALUES ('dvi', 'engli', 'T', '9.3')";
myStmt.executeUpdate(sql);
}
catch (Exception exc) {
exc.printStackTrace();
}
}
// Method to handle POST method request.
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
The index.jsp file is
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<H1>Welcome to Employee Management Project</H1>
<form action="FirstServlet" method="GET">
First Name: <input type="text" name="first_name"> <br />
Last Name: <input type="text" name="last_name" /> <br /> <br />
<input type="checkbox" name="add" /> Add <input type="checkbox" name="search" /> Search <input
type="checkbox" name="delete" /> Delete <input type="checkbox"
name="update" /> Update <br /> <br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
The web.xml file is
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>HelloServlet2</display-name>
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>first.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/FirstServlet</url-pattern>
</servlet-mapping>
</web-app>
Following is the hierarchy of the folder
HelloSevrvlet2
Java Resources
src
first
FirstServlet.java
Libraries
Apache Tomcat v7.0
EAR Libraries
JRE System Library
Web App Libraries
WebContent
WEB-INF
Lib
web.xml
index.jsp
action="/FirstServlet"- singhakashaction="FirstServlet". From/FirstServlet, the browser will construct an absolute URL likehttp://localhost/FirstServlet, which might or might not work depending on the mapping of the root of your application. On the other hand, action set toFirstServletwill result in an URL relative to the location ofindex.jsp(e.g.http://localhost/yourapproot/FirstServlet, which is what you actually want. - david a.//@WebServlet("/FirstServlet")try to uncomment this line - hece