method sendRedirect(String) is undefined

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)

http://img1.sycdn.imooc.com//climg/5d68d0af000165ec03361124.jpg

http://img1.sycdn.imooc.com//climg/5d68d0af0001150610410639.jpg

http://img1.sycdn.imooc.com//climg/5d68d0af0001632b08690832.jpg


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,这里是response.sendRedirect(),不是request。建议同学修改再试试。

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师