我是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中提到的字段将显示在相同的页面上。我要把它们印在新的页面上。
发布于 2016-10-25 06:27:29
您将需要创建WebServlet和post登录请求,然后使用HttpServletRequest,然后当您确定用户信息是正确的将用户转发到必要的页面request.getRequestDispatcher("index.jsp").forward(request, response);时,您将需要在登录表单中添加这样的东西,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");
}
} https://stackoverflow.com/questions/40232502
复制相似问题