0

Please I want to know how to insert values into MySQL Database using JSP and Servlets. This is my code below:

How do I input values to my database?

Index.jsp:

<%@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> <form name=”registration” action="SaveUser" method="post"> First Name: <input type="text" name="user_name"> <br /> Password: <input type="password" name="pass_word" /> <input type="submit" value="Submit" /> </form> </body> </html> 

And this is my Servet - SaveUser.java:

import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; 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 SaveUser extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("user_name"); String password = request.getParameter("pass_word"); System.out.println("The username is" + username); System.out.println("\nand the password is" + password); try { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdb", "root", "root"); String sql = "insert into usertable values (?,?)"; PreparedStatement prep = con.prepareStatement(sql); prep.setString(1, username); prep.setString(2, password); prep.executeUpdate(); prep.close(); } catch (Exception E) { System.out.println("The error is an error"); } } } 

I have Created my table, I have added the library. And it still gives me this error:

HTTP Status 404 - Not Found

type Status report

messageNot Found

descriptionThe requested resource is not available.

GlassFish Server Open Source Edition 4.0

I am working with netbeans. Is it a server problem, I really don't get the error, I'll appreciate every response to this problem.

2
  • Try to put your code inside doPost method not inside processRequest!! Commented May 4, 2014 at 14:37
  • Please tag Servlets and JSP. Commented May 4, 2014 at 14:37

2 Answers 2

1

Try this:

Add a doPost method to retrieve datas sent by the form + you need to add a servlet UrlPattern so you can access to the servlet from the browser, and to do that you can do it in web.xml file or just the annotations like the example below .

@WebServlet(description = "SaveUser", urlPatterns = {"/SaveUser"}) public class SaveUser extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Hello From deGet|SaveUser"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("user_name"); String password = request.getParameter("pass_word"); try { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdb", "root", "root"); String sql = "insert into usertable values (?,?)"; PreparedStatement prep = con.prepareStatement(sql); prep.setString(1, username); prep.setString(2, password); prep.executeUpdate(); prep.close(); System.out.println("Debug: data inserted!"); } catch (Exception E) { System.out.println("The error is an error"); } } } 
Sign up to request clarification or add additional context in comments.

Comments

1

Change your Servlet to this

public class ExampleServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RequestDispatcher rd = request.getRequestDispatcher("NewFile.jsp"); rd.forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("user_name"); String password = request.getParameter("pass_word"); try { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/newdb", "root", "root"); String sql = "insert into employee(ename,password) values (?,?)"; PreparedStatement prep = con.prepareStatement(sql); prep.setString(1, username); prep.setString(2, password); prep.executeUpdate(); prep.close(); System.out.println("Debug: data inserted!"); } catch (Exception E) { System.out.println("The error is an error " +E.getMessage()+ " "+E.getStackTrace()); } } } 

Add this in your web.xml

<servlet> <servlet-name>Submission</servlet-name> <servlet-class>com.example.test.ExampleServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Submission</servlet-name> <url-pattern>/SaveUser</url-pattern> </servlet-mapping> 

Run you Servlet as it should decide which JSP page should be loaded.

Also, I would suggest you use a DAO for Database Access.

1 Comment

you should mention that com.example.test.ExampleServlet must be changed to the Servlet packageName/className name, in this case servlet class is SaveUser so it could be com.example.test.SaveUser or whatever packageName OP is using

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.