简体   繁体   English

如何通过jsp将用户重定向到新的html页面?

[英]How to redirect the user to a new html page through jsp?

I have a html page called Login.html which is a simple login page with username and password and a submit button. 我有一个名为Login.html的html页面,它是一个简单的登录页面,其中包含用户名和密码以及一个提交按钮。 On click of the submit button a JSP page called Login.jsp is called which checks for the validity of username and password using SQL database. 单击提交按钮后,将调用一个名为Login.jsp的JSP页面,该页面使用SQL数据库检查用户名和密码的有效性。 The thing i wanted to do is If the USERNAME and PASSWORD are correct i wanted the REDIRECT the user to a new html page called Site.html . 我想做的是,如果USERNAME和PASSWORD正确,我希望将用户重定向到一个名为Site.html的新HTML页面。 But i cant find how to redirect to that page. 但是我找不到如何重定向到该页面。 The code of Login.jsp is Login.jsp的代码是

  try{
Connection con=null;
PreparedStatement stmt = null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:base","root","root");

String username= request.getParameter("uname");
String password= request.getParameter("pass");
String query = "SELECT * FROM users where uname=? AND pass=?";
stmt=con.prepareStatement(query);
stmt.setString(1,username);
stmt.setString(2,password);
ResultSet rs = stmt.executeQuery();

    if(rs.next())
   {
    out.println("Success");<%-- This is where i want to write the redirecting code --%>
   }
   else
   {
  out.println("Fail");
   }
 }
catch(Exception e)
{
 out.println(e);
}

request and response object's are implicitly available in jsp as well, just like in servlet. 与servlet一样, requestresponse对象在jsp中也隐式可用。 So you can do 所以你可以做

  if(rs.next())
   {
    response.sendRedirect(pathOfredirectingJSP);
   }
     if(rs.next())
     {
        out.println("Success");
        String redirectURL = "http://whatever.com/myJSPFile.jsp";
        response.sendRedirect(redirectURL);
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM