2017-10-15 62 views
0

請求調度程序中的轉發方法在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 /註冊。這是我想解決的問題

+0

看起來你錯過了很多相關的代碼。 – AHungerArtist

+0

我很驚訝,這個問題已經有3個答案。 – shinjw

+0

您需要發佈您的web.xml和更多的servlet類(包括方法簽名)。如果沒有這兩個重要的信息,任何人都無法給你一個真正的答案。 – shinjw

回答

0

請確保在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-完整文件。