簡體   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