繁体   English   中英

如何从一个JSP页面重定向到另一个新页面

[英]How to redirect from one JSP page to another fresh page

我是JSP和servlets的新手。 当点击“注册”按钮时,我编写了简单的jsp代码来显示注册表单,如下所示。 signup.jsp看起来像

<!DOCTYPE html>
<html>
<body>
<h2>Welcome </h2>
<img src="abcd.jpg" alt=" " style="width:500px;height:228px;">  
<form name="main"  method="get" action="login.jsp">
        <input type="submit" name="ter" value="SIGN IN" >
 </form>  
</body>
</html>

当我运行此signup.jsp时,会显示图像和“SIGN IN”按钮。 当我单击此按钮时,登录页面将显示在显示signup.jsp的同一页面上。 我如何修改jsp代码,以便SIGN IN详细信息将在新页面中而不是在同一页面上。 我的login.jsp看起来像

<%@ include file="index.jsp" %>  
<hr/>  

<h3>Login Form</h3>  
<%  
String profile_msg=(String)request.getAttribute("profile_msg");  
if(profile_msg!=null){  
out.print(profile_msg);  
}  
String login_msg=(String)request.getAttribute("login_msg");  
if(login_msg!=null){  
out.print(login_msg);  
}  
 %>  
 <br/>  
<form action="loginprocess.jsp" method="post">  
Email:<input type="text" name="email"/><br/><br/>  
Password:<input type="password" name="pass"/><br/><br/>  
<input type="submit" value="login"/>"  
</form>  

任何人都可以建议我如何使用JSP编写它,因为我对java脚本或其他技术也是全新的。

这就是我的servlet的样子:

@WebServlet("/SignInFunc")
@MultipartConfig
public class MySignInFunc extends HttpServlet {
    /** * */
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
        rd.forward(request, response);
    } 
}

当我点击登录时login.jsp中提到的字段将显示在同一页面上。 我希望他们在新页面上。

您需要创建WebServlet并发布登录请求,然后使用HttpServletRequest然后当您确定用户信息正确时将用户转发到必要的页面request.getRequestDispatcher("index.jsp").forward(request, response); 你还需要在你的登录表单中添加<form action = "login" method = "post">这类东西, WebServlet看起来像这样:

@WebServlet("/login")
public class HandleLogin extends HttpServlet {

    public HandleLogin() {
        super();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        ...
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        ...
        response.sendRedirect("/home.jsp");
    }
} 

暂无
暂无

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

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