method sendRedirect(String) is undefined
老师,请问我这个报错怎么解决? 错误提示说找不到这个sendRedirect方法...
个人当前eclipseEE版本为2019.6,MYSQL8,javaSE12
谢谢老师!
package com.imooc.web.action; 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 com.imooc.domain.User; import com.imooc.service.UserService; import com.imooc.service.impl.UserServiceImpl; /** * Servlet implementation class UserServlet */ //@WebServlet("/UserServlet") @WebServlet(value="/UserServlet") public class UserServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //get input String methodName = request.getParameter("method"); // if("login".equals(methodName)) { login(request,response); } } /** * UserServlet login * @param request * @param response */ private void login(HttpServletRequest request, HttpServletResponse response) { //get the username and password String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println(username+" "+password); //Data encapsulation User user = new User(); user.setUsername(username); user.setPassword(password); //process data UserService userService = new UserServiceImpl(); User existUser = userService.login(user); //page jump accroding to reuslt if(existUser == null) { //login failed //return to login page request.setAttribute("msg", "Wrong Username or Password!"); try { request.getRequestDispatcher("/admin/login.jsp").forward(request, response); } catch (ServletException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else { //login success //save user details and jump page request.getSession().setAttribute("existuser", existUser); request.sendRedirect(request.getContextPath()+"/admin/category_list.jsp"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
服务器Console里的报错也是这个原因
INFO: Server startup in [492] milliseconds Aug 30, 2019 12:25:21 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [com.imooc.web.action.UserServlet] in context with path [/shop1] threw exception [Servlet execution threw an exception] with root cause java.lang.Error: Unresolved compilation problem: The method sendRedirect(String) is undefined for the type HttpServletRequest at com.imooc.web.action.UserServlet.login(UserServlet.java:65) at com.imooc.web.action.UserServlet.doGet(UserServlet.java:28) at com.imooc.web.action.UserServlet.doPost(UserServlet.java:73) at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Thread.java:835)
2
收起
正在回答
1回答
同学你好,这里是response.sendRedirect(),不是request。建议同学修改再试试。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
3. Java 数据库开发与实战应用
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星