【问题标题】:How to redirect the user to a new html page through jsp?如何通过jsp将用户重定向到一个新的html页面?
【发布时间】:2014-04-17 10:05:42
【问题描述】:

我有一个名为 Login.html 的 html 页面,它是一个简单的登录页面,带有用户名和密码以及一个提交按钮。单击提交按钮后,将调用一个名为 Login.jsp 的 JSP 页面,该页面使用 SQL 数据库检查用户名和密码的有效性。 我想做的是如果 USERNAME 和 PASSWORD 正确,我希望将用户重定向到一个名为 Site.html 的新 html 页面。但我找不到如何重定向到该页面。 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);
}

【问题讨论】:

    标签: java html jsp


    【解决方案1】:

    requestresponse 对象在 jsp 中也隐式可用,就像在 servlet 中一样。所以你可以这样做

      if(rs.next())
       {
        response.sendRedirect(pathOfredirectingJSP);
       }
    

    【讨论】:

    • 做到了!不知道响应和请求是隐式可用的!谢谢! :)
    【解决方案2】:
         if(rs.next())
         {
            out.println("Success");
            String redirectURL = "http://whatever.com/myJSPFile.jsp";
            response.sendRedirect(redirectURL);
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多