I have written a code to add user into DB. I need to redirect to EmpInfo.jsp, when we receive duplicate entries. I need to use more Exceptions for that and also i want to know how to redirect.
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "cervlet";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "1234";
int Empid =Integer.parseInt(request.getParameter("Empid").toString());
String Name = request.getParameter("Name").toString();
int Age =Integer.parseInt(request.getParameter("Age").toString());
int Salary =Integer.parseInt(request.getParameter("Salary").toString());
PreparedStatement stmt;
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
//ArrayList al=null;
//ArrayList userList =new ArrayList();
String query = "insert into employee set Empid='"+Empid+"',name='"+Name+"',Age='"+Age+"',Salary='"+Salary+"'";
stmt = (PreparedStatement) conn.prepareStatement(query);
int i = 0;
try {
i = stmt.executeUpdate(query);
}
catch (SQLException e) {
String nextj = "/AddUser.jsp";
RequestDispatcher rd = getServletContext().getRequestDispatcher(nextj);
rd.forward(request, response);
}
System.out.println("i="+i);
System.out.println("query: " + query);
//if(i==0)
//{
//String nextj = "/EmpInfo.jsp";
//RequestDispatcher cd = getServletContext().getRequestDispatcher(nextj);
//cd.forward(request, response);
//response.sendRedirect("servletRecord");
//}
response.sendRedirect("/EmpInfo.jsp");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
Updates
I need to redirect to EmpInfo.jsp
, when it adds a new entry.
After the execution of the method executeUpadte()
, i used its return
value to redirect to another page called EmpInfo.jsp
.
But its not redirecting. I'm using eclipse. Tell me the common ways to multiple redirection of .jsp
pages.