请求调度程序转发方法不在servlet上执行post方法

时间:2017-10-15 05:17:35

标签: java jsp servlets forward requestdispatcher

在Servlet中的doGet()方法中创建时,请求调度程序中的 forward方法正常工作。但在用于dopost方法时不起作用。同样在Jsp文件中,我已将该方法声明为doPost。以下是我在servlet中的代码。

@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:在URL中使用signup.jsp时,它工作正常。但不适用于映射的URL / signUp。这是我试图解决的问题

1 个答案:

答案 0 :(得分:0)

确保在form标签method = post和你的servlet类中重写方法doPost(HttpServletRequest req,HttpServletResponse resp)。

Bydefault表单标记方法是get,你的servlet类需要覆盖方法doGet(HttpServletRequest req,HttpServletResponse resp)。

如果你不匹配,你会得到R.E- HTTP方法这个URL不支持GET(即-HTTP状态405错误) P.S-完整文件。