今天在学习尚硅谷的javaweb课中,在进行RegistServlet类的编写时,form表单中method="post"类中重写的方法为doPost,和视频上的代码一模一样,可还是报错405,如图所示。
package com.atguigu.web; import com.atguigu.pojo.User; import com.atguigu.service.UserService; import com.atguigu.service.impl.UserServiceImpl; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class RegistServlet extends HttpServlet { private UserService userService = new UserServiceImpl(); @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 1、获取请求的参数 String username = req.getParameter("username"); String password = req.getParameter("password"); String email = req.getParameter("email"); String code = req.getParameter("code"); // 2、检查 验证码是否正确 === 写死,要求验证码为:abcde if ("abcde".equalsIgnoreCase(code)) { // 3、检查 用户名是否可用 if (userService.existsUsername(username)) { System.out.println("用户名[" + username + "]已存在!"); // 跳回注册页面 req.getRequestDispatcher("/pages/user/regist.html").forward(req, resp); } else { // 可用 // 调用Sservice保存到数据库 userService.registUser(new User(null, username, password, email)); // // 跳到注册成功页面 regist_success.html req.getRequestDispatcher("/pages/user/regist_success.html").forward(req, resp); } } else { System.out.println("验证码[" + code + "]错误"); req.getRequestDispatcher("/pages/user/regist.html").forward(req, resp); } } }
报错为:
原因:
Apache、IIS、Nginx等绝大多数web服务器,都不允许静态文件响应POST请求
解决办法:
将POST请求换成GET请求即可 ,即将RegistServlet类中的doPost改为doGet,并令regist.html文件中的form表单中method="get"。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)