gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:07:14 24 4
gpt4 key购买 nike

我是 JSP 和 servlet 的新手。我编写了简单的 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 时,将显示图像和“登录”按钮。当我单击此按钮时,登录页面将显示在显示 signup.jsp 的同一页面上。我如何修改 jsp 代码,以便登录详细信息将出现在新页面而不是同一页面上。我的 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");
}
}

关于java - 如何从一个 JSP 页面重定向到另一个新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40232502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com