how to redirect to a jsp file from a servlet
843836Apr 4 2005 — edited Apr 4 2005hi,
i'm working on a school project and trying to build a job site. in the project i'm using a jsp file to register users which uses a html form and when the user presses on the submit button it go to a servlet and executes that. in the servlet there is a statement like
response.sendRedirect(response.encodeRedirectURL("login.jsp"));
my intension is to redirect the user to the login page after putting all the values to the database. from the servlet it can successfully put all the data into the database, but fails to redirect to login.jsp file.
here is my code-
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class UserReg extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response){
PrintWriter out = null;
String lastname=null;
String firstname=null;
String address=null;
String city=null;
String province=null;
String postalcode=null;
String phone=null;
String email=null;
boolean check=false;
String username=null;
String password=null;
String url = "jdbc:mysql:///project";
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
String query1=null;
String query2=null;
response.setContentType("text/html");
try {
out = response.getWriter();
lastname = request.getParameter("lastname");
firstname = request.getParameter("firstname");
address = request.getParameter("address");
city = request.getParameter("city");
province = request.getParameter("province");
postalcode = request.getParameter("postalcode");
phone = request.getParameter("phone");
email = request.getParameter("email");
username = request.getParameter("username");
password = request.getParameter("password");
} catch (IOException e) {
e.printStackTrace();
}
query1="INSERT INTO userlog VALUES('"+username+"','"+password+"')";
query2="INSERT INTO jobseeker VALUES('"+username+"','"+lastname+"','"+firstname+"','"+
address+"','"+city+"','"+province+"','"+postalcode+"','"+phone+"','"+email+"')";
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
// Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(Exception e){}
try{
connection=DriverManager.getConnection(url,"root","shahed");
statement=connection.createStatement();
statement.executeUpdate(query1);
statement.executeUpdate(query2);
check=true;
}catch(SQLException e){}
try{
connection.close();
}catch(SQLException e){}
if(check){
try{
response.sendRedirect(response.encodeRedirectURL("login.jsp"));
}catch(Exception e){}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response){
doGet(request,response);
}
}
can anyone please help me out?
thanks inadvance for your help.