请求调度程序转发方法不工作在servlet上的post方法
问题描述:
请求调度程序中的转发方法在Servlet中的doGet()方法中创建时工作正常。但在dopost方法中使用时不起作用。同样在Jsp文件中,我已经将方法声明为doPost。以下是我在servlet中的代码。请求调度程序转发方法不工作在servlet上的post方法
@WebServlet(name = "SignUpServlet")
public class SignUpServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("lk/ruh/uniAssist/spring/SpringXMLConfig.xml");
SignUPService signUpservice = (SignUPService) ctx.getBean("signUpservice");
String username = request.getParameter("username");
String studentName = request.getParameter("studentName");
String regNo = request.getParameter("regNo");
String pwd = request.getParameter("pwd");
String department = request.getParameter("department");
String semester = request.getParameter("semester");
System.out.println(username);
System.out.println(studentName);
System.out.println(regNo);
System.out.println(pwd);
System.out.println(department);
System.out.println(semester);
RequestDispatcher requestDispatcher = request.getRequestDispatcher("signup.jsp");
requestDispatcher.include(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
这正是在doPost方法的方式。以上格式时没有任何反应。但是当我在doGet方法中声明RequestDispatcher方法时,它工作正常。这背后的原因是什么?
<div class="login-bottom">
<h2>Register</h2>
<form name="signUpForm" id="signup" method="post" action="/signUp">
<div class="col-md-6">
<div class="login-mail">
<input type="text" name="username" placeholder="Username" required="">
<i class="fa fa-user"></i>
</div>
<div class="login-mail">
<input type="text" name="studentName" placeholder="Name with initials" required="">
<i class="fa fa-male"></i>
</div>
<div class="login-mail">
<input type="text" name="regNo" placeholder="RegNo (ex:2528)" required="">
<i class="fa fa-pencil"></i>
</div>
<div class="login-mail">
<input type="password" name="pwd" placeholder="Password" required="">
<i class="fa fa-lock"></i>
</div>
<div class="login-mail" placeholder="Department">
Department
<select name="department" id="depSelector" class="form-control1">
<option>Civil and Environment Eng.</option>
<option>Electrical and Information Eng.</option>
<option>Mechanical and Manufacturing Eng.</option>
</select>
</div>
<div class="login-mail" placeholder="Department">
Semester
<select name="semester" id="semSelector" class="form-control1">
<option value="1">1 Semester</option>
<option value="2">2 Semester</option>
<option value="3">3 Semester</option>
<option value="4">4 Semester</option>
<option value="5">5 Semester</option>
<option value="6">6 Semester</option>
<option value="7">7 Semester</option>
<option value="8">8 Semester</option>
</select>
</div>
</div>
<div class="col-md-6 login-do" align="center">
<img src="images/icon.png" style="height: 200px; width: 200px;">
<label class="hvr-shutter-in-horizontal login-sub">
<input type="submit" value="Submit">
</label>
<p>Already register</p>
<a href="signin.jsp" class="hvr-shutter-in-horizontal">Login</a>
</div>
<div class="clearfix"></div>
</form>
</div>
这里是有关JSP代码。
我的整个网站的XML代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--servlet declaration-->
<servlet>
<servlet-name>TimeTableServlet</servlet-name>
<servlet-class>lk.ruh.uniAssist.servlet.TimeTableServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>coursesServlet</servlet-name>
<servlet-class>lk.ruh.uniAssist.servlet.coursesServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>SignInServlet</servlet-name>
<servlet-class>lk.ruh.uniAssist.servlet.SignInServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>SignUpServlet</servlet-name>
<servlet-class>lk.ruh.uniAssist.servlet.SignUpServlet</servlet-class>
</servlet>
<!--Servlet Mapping-->
<servlet-mapping>
<servlet-name>TimeTableServlet</servlet-name>
<url-pattern>/timeTable</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>coursesServlet</servlet-name>
<url-pattern>/courses</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SignInServlet</servlet-name>
<url-pattern>/signIn</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SignUpServlet</servlet-name>
<url-pattern>/signUp</url-pattern>
</servlet-mapping>
PS:当signup.jsp在URL中使用它工作正常。但不适用于映射的URL /注册。这是我想解决的问题
答
请确保在form tag method = post和您的servlet类中,覆盖方法doPost(HttpServletRequest req,HttpServletResponse resp)。
默认的表单标签方法是get和你的servlet类需要重写方法doGet(HttpServletRequest req,HttpServletResponse resp)。
如果您不匹配,您将得到R.E- HTTP方法GET不受此URL支持(即-HTTP状态405错误) P.S-完整文件。
看起来你错过了很多相关的代码。 – AHungerArtist
我很惊讶,这个问题已经有3个答案。 – shinjw
您需要发布您的web.xml和更多的servlet类(包括方法签名)。如果没有这两个重要的信息,任何人都无法给你一个真正的答案。 – shinjw