[英]Calling a servlet from Java code
我目前正在嘗試使用jsp和servlets完成登錄系統。我的注冊和登錄驗證工作正常。
我希望在登錄后鏈接到歡迎頁面以顯示用戶個人資料信息。 出於測試目的,我有一個response.sendRedirect(welcome.jsp),它在登錄servlet成功登錄后重定向。
現在,要在此歡迎頁面上顯示配置文件信息,我將使用servlet從數據庫中收集信息並使用printwriter將其呈現給瀏覽器。 如何從loginservlet成功調用此servlet以運行doPost()方法? 或者有更好的方法嗎?
感謝您的時間。
(為簡單起見,我只是試圖讓一個基本的網頁首先出現,以確保這是有效的,一旦我得到這個,我將對數據庫方面沒有任何問題)
登錄服務器:
package logon;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try
{
System.out.println("In the Login Servlet");
User user = new User();
user.setEmail(request.getParameter("email"));
user.setPassword(request.getParameter("password"));
LoginDAO.login(user);
if(user.isValid())
{
HttpSession session = request.getSession(true);
session.setAttribute("currentSessionUser",user);
session.setAttribute("currentSessionUserEmail", user.getEmail());
response.sendRedirect("WelcomeServlet");
}else
response.sendRedirect("LoginFailure.html");
} catch (Throwable exc)
{
System.out.println(exc);
}
}
}
歡迎服務:
package logon;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/WelcomeServlet")
public class WelcomeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public WelcomeServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.print("<html>"+"<head>"+"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">");
out.print("<title>Welcome</title>");
out.print("</head>"+"<body>");
out.print("Welcome to the welcome page!!!");
out.print("</body>"+"</html>");
}
}
您不能使用POST重定向,只能使用GET。 因為您只是在WelcomeServlet中顯示HTML,所以將代碼從doPost移動到doGet,或者讓一個調用另一個,這簡單地使它們都做同樣的事情。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
此外,這將是更好地使用JSP比out.print
一個servlet里面一堆的HTML。 請參閱servlet的信息頁面 。
另外,顯然您的歡迎頁面需要從會話中讀取屬性currentSessionUser
並確保它不為null
以查看用戶是否確實已登錄。 否則,如果用戶知道歡迎頁面的地址,他們就可以繞過您的登錄檢查,就像現在一樣。
您的問題是您當前已實現Servlet以響應錯誤的HTTP謂詞。
您會注意到servlet具有doPost
和doGet
方法。 正如您可能希望這些映射到HTTP GET和HTTP POST請求。 您當前的問題源於這樣一個事實:您已經在WelcomeServlet
實現了doPost
方法,因此期望POST請求,當它實際上將提供GET請求時。
粗略地說,您可以將GET請求視為讀取操作,將POST請求視為寫入操作。 因此,當您提交表單以保存某些數據時,通常會處理POST請求。 您基本上要求將數據寫入數據庫或會話。 加載網頁時,通常將其作為GET請求處理。 您只是要求閱讀數據。
再次簡化,但重定向通常是GET請求。 因此,您的Servlet需要實現doGet()
方法,以便在重定向后響應瀏覽器GET請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.