Issue
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?
Solution
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.
Answered By - Edwin