我试图从一个servlet的doPost方法转发到另一个servlet的doGet方法。但是,当我运行网络应用时,网址无法更改。当我手动输入第二个servlet的URL时,servlet完美地工作。当我在RequestDispatcher中输入网页时,它也可以工作。
第一个servlet是一个loginservlet。此servlet的doPost方法处理用户的数据库查找。接下来,根据用户的类型(艺术家,听众或管理员),servlet通过第二个servlet的doGet方法重定向到相应的主页。最后一步是必需的,不能跳过,因为我将在此方法中编写更多代码。
这是登录servlet:
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("/login.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
if(request.getParameter("username") != null && request.getParameter("password") != null &&
request.getParameter("username") != "" && request.getParameter("password") != ""){
//Check if username exists
UserHelper helper = new UserHelper();
if(helper.userExists(request.getParameter("username")) == 1){
User user = helper.getUserByUsername(request.getParameter("username"));
if(user.getUserPassword().equals(helper.hashPassword(request.getParameter("password")))){
HttpSession session = request.getSession();
session.setAttribute("user", request.getParameter("username"));
if(user instanceof Artist){
Artist artist = new Artist();
artist = (Artist) user;
session.setAttribute("Artist", artist);
System.out.println("OK");
request.getRequestDispatcher("/artist/artistpanel").forward(request, response);
}
if(user instanceof Listener){
Listener listener = new Listener();
listener = (Listener) user;
session.setAttribute("Listener", listener);
request.getRequestDispatcher("/listener/store.jsp").forward(request, response);
}
if(user instanceof Admin){
Admin admin = new Admin();
admin = (Admin) admin;
session.setAttribute("Admin", admin);
}
/*response.sendRedirect("artist/artisthome.jsp?success=login");*/
}else{
request.setAttribute("error", "U gaf een foutief passwoord op. Probeer nogmaals.");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}else{
request.setAttribute("error", "Er bestaat geen gebruiker met die username.");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}else{
request.setAttribute("error", "Beide velden moeten worden ingevuld.");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
}
这里是ControlPanel servlet代码:
package Controller.Servlets;
import Model.Artist;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
*
* @author Daan
*/
public class ArtistPanelServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Artist artist = new Artist();
HttpSession session = request.getSession();
artist = (Artist) session.getAttribute("Artist");
session.setAttribute("Artist", artist);
request.setAttribute("ArtistName", artist.getArtistName());
request.getRequestDispatcher("/artist/artisthome.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
@Override
public String getServletInfo() {
return "Short description";
}
}
这是我的servlet映射的一部分。
<servlet>
<servlet-name>ArtistPanelServlet</servlet-name>
<servlet-class>Controller.Servlets.ArtistPanelServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ArtistPanelServlet</servlet-name>
<url-pattern>/artist/artistpanel</url-pattern>
</servlet-mapping>
谢谢你的帮助。
答案 0 :(得分:1)
将它重定向到doGet的唯一方法是让你的doPost调用doGet,这使得它们都做同样的事情:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
doGet(request, response);
}