1

I am running a example of form in which i have three fields 'name', 'email', 'pass'. Now i want to display the values which are entered by the user, along with the message "You are successfully registered". I'm using tomcat 8.5, JDK 1.8 and SQL Community 5.6

index.html

<html>
    <head>
        <title>Register form</title>
    </head>
    <body>
        <form method="post" action="register">
        Name:<input type="text" name="name" /><br/>
        Email ID:<input type="text" name="email" /><br/>
        Password:<input type="text" name="pass" /><br/>
        <input type="submit" value="register" />
        </form>
    </body>
</html>

Register.java

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

public class Register extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String pass = request.getParameter("pass");
        try{

        //loading drivers for mysql
        Class.forName("com.mysql.jdbc.Driver");

    //creating connection with the database 
          Connection  conn=DriverManager.getConnection
                     ("jdbc:mysql://localhost:3306/studentdb","root","root");

        PreparedStatement ps=conn.prepareStatement
                  ("insert into Student values(?,?,?)");

        ps.setString(1, name);
        ps.setString(2, email);
        ps.setString(3, pass);
        int i=ps.executeUpdate();

          if(i>0)
          {
            out.println("You are sucessfully registered");
          }
          String query = "SELECT * FROM student";

      // create the java statement
      Statement st = conn.createStatement();

      // execute the query, and get a java resultset
      ResultSet rs = st.executeQuery(query);

      // iterate through the java resultset
      while (rs.next())
      {
                    rs.getString("name");
                    rs.getString("email");
                    rs.getString("pass");


        // print the results
        System.out.format("%s, %s, %s\n", name, email, pass);
         System.out.print("name: " + name);
         System.out.print(", email: " + email);
         System.out.print(", pass: " + pass);

      }

      st.close();

        }
        catch(Exception se)
        {
            se.printStackTrace();
        }

     }
  }

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" >

    <servlet>
        <servlet-name>register</servlet-name>
        <servlet-class>Register</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>register</servlet-name>
        <url-pattern>/register</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

Now What code should i add in order to display the user entered values on the screen?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Hanut
  • 39
  • 8

1 Answers1

-1

First of all ,Because of you used Servlet, you need to use JSP instead of html;


and then, you should make sure your form action's path;

<form action="${ pageContext.request.contextPath }/register" method="post">

if you don't use EL Expression,try to use the following one

 <form action="/register" method="post">

java:

String name = request.getParameter("name");
String email = request.getParameter("email");
String pass = request.getParameter("pass");
System.out.print("name: " + name);
System.out.print(", email: " + email);
System.out.print(", pass: " + pass);

Lastly,you'd better use the MVC later.

Edwin
  • 199
  • 1
  • 4
  • 1
    what code i need to write in JSP and where to put
    plz explain ?
    – Hanut Nov 21 '17 at 13:12
  • All of the things that your code write on HTML need to put on the JSP,and
    put on the
    – Edwin Nov 21 '17 at 13:17
  • if you use EL Expression,you should put the <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> on the top of your JSP,and than you can use
    to instead
    – Edwin Nov 21 '17 at 13:20
  • If you use the latter one, I can make sure your path is right – Edwin Nov 21 '17 at 13:21
  • No, i'm not using EL expression. i did simply put all the content of HTML file into JSP with above form action. Now next? – Hanut Nov 21 '17 at 13:25
  • Pay attention to add on the top of JSP like this :<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> – Edwin Nov 21 '17 at 13:28
  • And then, as my answer says.add some code in you java servlet to let you see the return value – Edwin Nov 21 '17 at 13:30
  • Remember to run the entire project on TOMCAT rather than just a single JSP. – Edwin Nov 21 '17 at 13:32
  • <%@page language="java" import="java.util.*" encoding="UTF-8"%> <%@taglib uri="java.sun.com/jsp/jstl/core";prefix="c"%> I have add these two lines on the top of JSP page. Is it ok? – Hanut Nov 21 '17 at 13:39
  • oops, sorry ,if you use EL expression, you must add these,bu you don't want to use it so you can Ignore these code unless you encounter 404 errors.But it doesn't go wrong normally. – Edwin Nov 21 '17 at 13:51
  • where to add above java servlet code in my code? – Hanut Nov 21 '17 at 14:08
  • just add these code before try-catch block to make sure values output in the console.This can display information even if your database connection goes wrong. – Edwin Nov 21 '17 at 14:22
  • I have some confusions like where else i have to make change according to JSP page, In my main servlet page. – Hanut Nov 21 '17 at 16:19