Why am I getting a blank page in my servlet program - java

I am using netbeans 8.2 and using Apache Tomcat server. Whenever I run the program I get to the registration page. But when I submit the information I am supposed to get an output. But I always get a blank page.I am using the database present in JAVA. Please Help I have to submit my java project;_;. I still need to create a login page.
Thanks in Advance.
Jsp Program
<%--
Document : registeration
Created on : Mar 12, 2019, 10:24:50 AM
Author : Pranav Sharma
--%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<div align="center">
<form action="Registeration" method="POST">
User Name:<input type="text" name="user" required="required"><br>
Password : <input type="password" name="password"
required="required"><br>
Age : <input type="text" name="age" required="required" /><br>
Gender : <select name="gender">
<option>Male</option>
<option>Female</option>
<option>Transgender</option>
</select><br>
Event : <select name="event" multiple="multiple">
<option>Mr.Tanwar Body Building</option>
<option>Fashion Show</option>
<option>Dance</option>
<option>Singing</option>
<option>Coding</option>
</select><br>
<input type="submit" value="REGISTER" />
<input type="reset" value="RESET" />
</form>
</div>
</body>
Servlet Program
package jdbc;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet(name="Registeration", urlPatterns={"/Registeration"})
public class Registeration extends HttpServlet
{
private static final long serialVersionUID = 1L;
public Registeration(){
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
try {
String name = request.getParameter("user");
String password = request.getParameter("password");
String age = request.getParameter("age");
String gender = request.getParameter("gender");
String event = request.getParameter("event");
String sql = "insert into
registeration(name,password,age,gender,event) values(?,?,?,?,?)";
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/RegForm","pranav","sharma");
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,password);
ps.setString(3,age);
ps.setString(4,gender);
ps.setString(5,event);
ps.executeUpdate();
PrintWriter out = response.getWriter();
out.println("You have successfully registered!");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
Web-xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>Registeration</servlet-name>
<servlet-class>jdbc.Registeration</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Registeration</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

I have create a java-servlet project which uses sql server as a database. Underline project takes input from the ui and persist into database.
After inserting data into database, it will show up is records inserted successfully or not.
Change the DBConnectionUtils according to your database
String url = "jdbc:mysql://localhost:3306/";
String dbName = "root";
String driver = "com.mysql.jdbc.Driver";
This is the link for the project on github https://github.com/rosmahajan/java-servlet
Otherwise please post more information like any exception or error you are getting to investigate what is wrong with your code.
Hope this helps you !!

Related

Delete a record from database using servlet

The Remove.java is the servlet and the index.jsp is the jsp file. I am trying to delete the file using email,since it is unique.
Remove.java
package servletPool;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet("/Remove")
public class Remove extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String uEmail = request.getParameter("email");
try {
Class.forName("com.mysql.jdbc.Driver");
String user = "root";
String pass = "root";
String query = "delete from user_details where email=?";
Connection con = DriverManager.getConnection("jdbc:mysql://locahost:3306/dbname", user, pass);
PreparedStatement ps = con.prepareStatement("delete from user_details where email=?");
ps.setString(1, uEmail);
int i = ps.executeUpdate();
if(i > 0) {
out.println("User successfully removed...");
}
} catch (Exception e) {
System.out.println(e);
}
}
}
and here is the index.jsp file from which the record will be deleted.
index.jsp
<%# 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>Registration Page.</title>
</head>
<body>
<div>
<div style="padding : 10px;">
<center>
<form action="Remove.servlet" method="get">
<h5>If you want to remove users,</h5>
<input type="email" placeHolder="User's Email" name="email"/>
<input type="button" value="Click" name="remove"/>
</form>
</center>
</div>
</center>
</div>
</body>
</html>
Try changing from:
<form action="Remove.servlet" method="get">
to:
<form action="Remove" method="post">
and from:
<input type="button" value="Click" name="remove"/>
to:
<input type="submit" value="Click" name="remove"/>
In addition, line 26 (</center>) in index.jsp and line 29 (String query = "delete from user_details where email=?";) in Remove.java should be removed because they are unnecessary.

“HTTP Status 404 The requested resource is not available”

I'm newbie to servlet programming. I have checked various other links about the Http 404 error but nothing helped me. So I'm posting my code here.
I have three html forms in WebContent/ folder form1.html, form2.html,form3.html, all these forms have same url pattern because accessing three different forms in the same Http session.
form1.html
<html>
<head>
<title>Adhar Registration Form</title>
</head>
<body style="background-color:orange">
<h1>FORM 1</h1>
<form action="./reg" method="get">
<table>
<tr><td>NAME:</td><td><input type="text" name="id"/></td></tr>
<tr><td>F_NAME:</td><td><input type="text" name="name"/></td></tr>
<tr><td>M_NAME:</td><td><input type="text" name="email"/></td></tr>
<tr><td><input type="submit" name= "NEXT"> </td></tr>
</table>
<input type="hidden" name="fno" value="1">
</form>
</body>
</html>
form2.html
<html>
<head>
<title>Adhar Registration Form</title>
</head>
<body style="background-color:orange">
<h1>FORM 2</h1>
<form action="./reg" method="get">
<table>
<tr><td>CONTACT:</td><td><input type="text" name="id"/></td></tr>
<tr><td>EMAIL:</td><td><input type="text" name="name"/></td></tr>
<tr><td>ADDRESS:</td><td><textarea rows ="10" cols="5" name="address"></textarea></td></tr>
<tr><td><input type="submit" name= "NEXT"> </td></tr>
</table>
<input type="hidden" name="fno" value="2">
</form>
</body>
</html>
<html>
<head>
<title>Adhar Registration Form</title>
</head>
<body style="background-color:orange">
<h1>FORM 3</h1>
<form action="./reg" method="get">
<table>
<tr><td>QUALIFICATION:</td><td><input type="text" name="id"/></td></tr>
<tr><td>PAN NO:</td><td><input type="text" name="name"/></td></tr>
<tr><td><input type="submit" name= "Register"> </td></tr>
</table>
<input type="hidden" name="fno" value="3">
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Adhar</display-name>
<welcome-file-list>
<welcome-file>form1.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>container.RegistrationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/reg</url-pattern>
</servlet-mapping>
</web-app>
RegistrationServlet.java
package container;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
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 javax.servlet.http.HttpSession;
/**
* Servlet implementation class RegistrationServlet
*/
#WebServlet("/reg")
public class RegistrationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public static final String URL = "jdbc:mysql://localhost/db";
public static final String USER = "root";
public static final String PASSWORD = "12345";
public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
/**
* #see HttpServlet#HttpServlet()
*/
public RegistrationServlet() {
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
PrintWriter out = response.getWriter();
HttpSession hs = request.getSession();
String fno = request.getParameter("fno");
if(fno.equals("1")){
String name = request.getParameter("name");
String f_name = request.getParameter("f_name");
String m_name = request.getParameter("m_name");
hs.setAttribute("name", name);
hs.setAttribute("f_name", f_name);
hs.setAttribute("m_name", m_name);
response.sendRedirect("./Form2.html");
}
if(fno.equals("2")){
String contact = request.getParameter("contact");
String email = request.getParameter("email");
String address = request.getParameter("address");
hs.setAttribute("contact", contact);
hs.setAttribute("email", email);
hs.setAttribute("address", address);
response.sendRedirect("./Form3.html");
}
if(fno.equals("3")){
String qual = request.getParameter("qual");
String pan = request.getParameter("pan");
String name = (String)hs.getAttribute("name");
String f_name = (String)hs.getAttribute("f_name");
String m_name = (String)hs.getAttribute("m_name");
String contact = (String)hs.getAttribute("contact");
String email = (String)hs.getAttribute("email");
String address = (String)hs.getAttribute("address");
try {
Class.forName(DRIVER_CLASS);
System.out.println("Loaded the driver");
Connection con = DriverManager.getConnection(URL,USER,PASSWORD);
PreparedStatement ps = con.prepareStatement("INSERT into adharReg values(?,?,?,?,?,?,?,?)");
ps.setString(1, name);
ps.setString(2, f_name);
ps.setString(3, m_name);
ps.setString(4, contact);
ps.setString(5, email);
ps.setString(6, address);
ps.setString(7, qual);
ps.setString(8, pan);
int i = ps.executeUpdate();
if(i!=0){
out.println("<h1>REGISTRATION SUCCESS</h1>");
}
else{
out.println("<h1>REGISTRATION FAILED</h1>");
}
} catch (Exception e) {
// TODO: handle exception
out.println("<h1>REGISTRATION FAILED" + e.getMessage() + " </h1>");
}
}
}
}
I'm using tomcat server 8.0. I checked this Link and my tomcat server has exact same settings. And followed this link for any possible mistakes but, I don't know the exact reason why i'm getting Http 404 error. Help me out why i'm getting this error.
Thanks.
The issue is caused by a conflict between web.xml configuration and the WebServlet annotation. The web.xml is delcaring a servlet called login that target to the /reg url, but also in the RegistrationServlet there is a declaration throught #WebServlet annotation that reference to the same url /reg.
One possible solution is to remove the servlet declaration from the web.xml, that means that the web.xml content should be like this.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Adhar</display-name>
<welcome-file-list>
<welcome-file>form1.html</welcome-file>
</welcome-file-list>
</web-app>
Just let the Servlet declared by annotation only. Hope this helps.
check web.xml is exist into WEB-INF folder.
and use servlet mapping into web.xml
And register the servlet instead in web.xml like this:
<servlet>
<servlet-name>yourServlet</servlet-name>
<servlet-class>com.example.YourServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>yourServlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
for more refer bellow link :-
http://www.beingjavaguys.com/2013/08/jsp-servlet-hello-world-example.html
https://www.javatpoint.com/servlet-with-annotation

HTTP Status 405 - HTTP method GET is not supported by this URL.The specified HTTP method is not allowed for the requested resource

Help me here guys...I have added servlet jar file and mysql connector jar file but it's showing the error like HTTP Status 405 - HTTP method GET is not supported by this URL.The specified HTTP method is not allowed for the requested resource.I have attempted every possible solution but i am getting the same error.
Regform.java
package com.servlet.info;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import com.mysql.jdbc.Driver;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Regform extends HttpServlet {
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String fNM = request.getParameter("firstName");
String lNM = request.getParameter("lastName");
String eID = request.getParameter("emailID");
String uNM = request.getParameter("userName");
String pass = request.getParameter("password");
try{
//loading drivers for mysql
com.mysql.jdbc.Driver mySqlDriverClassRef = new com.mysql.jdbc.Driver();
DriverManager.registerDriver(mySqlDriverClassRef);
//creating connection with the database
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_form?&useSSL=false", "j2ee" ,"j2ee");
PreparedStatement ps=con.prepareStatement
("insert into student values(?,?,?,?,?);");
ps.setString(1, fNM);
ps.setString(2, lNM);
ps.setString(3, eID);
ps.setString(4, uNM);
ps.setString(5, pass);
int i=ps.executeUpdate();
if(i>0)
{
out.println("You are sucessfully registered");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
index.html
<html>
<head>
<title>Register form</title>
</head>
<body>
<form action="/register" method="POST" >
firstName:<input type="text" name="firstName" /><br/>
lastName:<input type="text" name="lastName" /><br/>
emailID :<input type="text" name="email" /><br/>
userName:<input type="text" name="userName" /><br/>
Password:<input type="text" name="pass" /><br/>
<input type="submit" value="register" />
</form>
</body>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd" >
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Regform</servlet-name>
<servlet-class>com.servlet.info.Regform</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Regform</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
Try adding below to your Java and see if it works
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws
IOException,
ServletException {
doPost(request, response);
}
I recommend that you always write the codes you defined in the doPost and doGet methods in the doProcess method.

Sign In page using SQL SERVER, JAVA, Servlets, Eclipse

Here is my code, it is connecting to database, in the console I could see the msg "connected" but the problem is after giving the user credentials it is not navigating to the next page(welcome.jsp) and it just shows a msg that "username or password error" could someone help me out
LoginServlet.java
package com.amzi.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("username");
String p=request.getParameter("userpass");
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://WMDENTW1\\SQLEXPRESS:1433;" +
"database=FullTextDB;" +
"user=root;" +
"password=root123";
Connection conn = DriverManager.getConnection(connectionUrl);
System.out.println("Connected.");
PreparedStatement pst = conn.prepareStatement("select User, Password from dbo.AdminLogin where User=? and Password=?");
pst.setString(1, n);
pst.setString(2, p);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
out.println("page opened");
RequestDispatcher rd=request.getRequestDispatcher("WebContent/welcome.jsp");
rd.forward(request,response);
} else {
out.print("<p style=\"color:red\">Sorry username or password error</p>");
RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
rd.include(request,response);
}
} catch (Exception e) {
e.printStackTrace();
}
out.close();
}
}
index.JSP
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Application</title>
</head>
<body>
<form action="LoginServlet" method="post">
<fieldset style="width: 300px">
<legend> Login to App </legend>
<table>
<tr>
<td>User ID</td>
<td><input type="text" name="username" required="required" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="userpass" required="required" /></td>
</tr>
<tr>
<td><input type="submit" value="Login" /></td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>
Welcome.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Welcome <%=session.getAttribute("name")%></title>
</head>
<body>
<h3>Login successful!!!</h3>
<h4> Hello, <%=session.getAttribute("name")%></h4>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" version="2.5"> <servlet> <servlet-name>login</servlet-name> <servlet-class>com.amzi.servlets.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
</web-app>
In response your your request to clarify my comment, instead I shall write it as an answer.
IN your doPost method, you have this line:
PrintWriter out = response.getWriter();
After that, you use out to write directly to the response, such as in these lines:
out.println("page opened");
and
out.print("<p style=\"color:red\">Sorry username or password error</p>");
However, you then go and forward to a JSP:
RequestDispatcher rd=request.getRequestDispatcher("WebContent/welcome.jsp");
rd.forward(request,response);
But that does the same thing - after processing the JSP it sends the result to the response PrintWriter, just have you have done.
You really shouldn't do both. Just use the JSPs.
Secondly, your message 'username or password error' indicates that the query returned no rows, so the username/password you used is probably wrong. I can't see anything wrong with the JDBC code or query.

JSP Form Not Connecting with Servlet 404-Error

I'm having a little issue connecting with my servlet so that I can pass some data to a mysql database. I've read a bunch of the threads here, but have had no luck with suggestions to other members.
I have a jsp page named "insertData.jsp" On that page there is a form where the action points to a servlet named "UpdateData". When I click submit on the web page, I get a 404 error stating that the requested resource is not available. I have also updated my web xml file to try to point to the right direction.
So here's my folder setup:
The UpdateData.java is in the controller package of the source packages folder. The name of the project is "RukertContainerTracker".
Here's my jsp page:
<%#taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%#page import="java.sql.ResultSet"%>
<%#page import="java.sql.Statement"%>
<%#page import="java.sql.DriverManager"%>
<%#page import="java.sql.Connection"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert Data</title>
</head>
<body>
<h1>Insert Data Into Container Records</h1>
<H1>The Rukert Terminals Container Tracker </H1>
<form name="Insert Record" action="/UpdateData" method="Post">
Container Number: <input type="text" name="containerNumber"> <br />
Full Out: <input type="date" name="fullOut" /> <br/>
Empty In: <input type="date" name="emptyIn" /> <br/>
Empty Out <input type="date" name="emptyOut" /> <br/>
Full In: <input type="date" name="fullIn" /> <br/>
Comments: <input type="text" name="comments" /> <br/>
<input type="submit" value="Submit" />
</form>
<div>
<a href="javascript:history.back();">
<span class="categoryLabelText">HOME</span>
</a>
</div>
</body>
</html>
My servlet:
package controller;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UpdateData extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
//Get container data from the JSP page
String container=request.getParameter("containerNumber");
String fullOutDate=request.getParameter("fullOut");
String emptyInDate=request.getParameter("emptyIn");
String emptyOutDate=request.getParameter("emptyOut");
String fullInDate=request.getParameter("fullIn");
String comments=request.getParameter("comments");
//Print the above got values in console
System.out.println("The username is" +container);
System.out.println("\nand the password is" +fullOutDate);
String connectionparams="jdbc:mysql://localhost:3306/rukerttracker";
String db="rukerttracker";
String uname="root";
String psword="Colorado1982";
Connection connection=null;
ResultSet rs;
try {
// Loading the available driver for a Database communication
Class.forName("com.mysql.jdbc.Driver");
//Creating a connection to the required database
connection = DriverManager.getConnection
(connectionparams, uname, psword);
//Add the data into the database
String sql = "insert into containerinventory values (?,?,?,?,?,?)";
try (PreparedStatement prep = connection.prepareStatement(sql)) {
prep.setString(1, container);
prep.setString(2, fullOutDate);
prep.setString(3, emptyInDate);
prep.setString(4, emptyOutDate);
prep.setString(5, fullInDate);
prep.setString(6, comments);
prep.executeUpdate();
}
}catch(Exception E){
System.out.println("The error is=="+E.getMessage());
}
finally{
connection.close();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userPath = request.getServletPath();
// if category page is requested
if (userPath.equals("/insertData")) {
// TODO: Implement category request
// use RequestDispatcher to forward request internally
String url = "/WEB-INF/view" + userPath + ".jsp";
try {
request.getRequestDispatcher(url).forward(request, response);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
And finally my web.xml page:
<servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>controller.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/chooseLanguage</url-pattern>
<url-pattern>/viewTracker</url-pattern>
<url-pattern>/editTracker</url-pattern>
<url-pattern>/addToCart</url-pattern>
<url-pattern>/viewCompany</url-pattern>
<url-pattern>/category</url-pattern>
<url-pattern>/updateCart</url-pattern>
<url-pattern>/purchase</url-pattern>
<url-pattern>/viewCart</url-pattern>
<url-pattern>/checkout</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>UpdateData</servlet-name>
<servlet-class>controller.UpdateData</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UpdateData</servlet-name>
<url-pattern>/insertData</url-pattern>
</servlet-mapping>
I have two servlets, I don't know if this matters, but I couldn't get the application to work in the controller servlet, so I created the Update Data servlet.
Any help as to why I keep getting this 404 error would be greatly, greatly appreciated. Thanks for taking the time to look at this.
I think in form you are using POST method and your servlet does not have post method. please check it.
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{...}
not available.

Categories