2013-07-07 47 views
2

我在JSP初學者和我有以下的servlet:的doPost(GAE的servlet)不起作用

@SuppressWarnings("serial") 
public class HelloAppIgorServlet extends HttpServlet { 
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { 
     RequestDispatcher disp = req.getRequestDispatcher("/mainpage.jsp"); 
     disp.forward(req, resp); 
    } 

    @Override 
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
     PrintWriter pw = resp.getWriter(); 
     pw.print("Test"); 
     pw.close(); 
    } 
} 

和一個JSP文件調用mainpage.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
    <p>Hello there!<p> 
    <form action="." method="post"> 
     <input type="text" name="name1" /> 
     <input type="submit" value="name2" /> 
    </form> 
</body> 
</html> 

的問題是doPost()方法不起作用。當我點擊按鈕時,它會將我重定向到index.html頁面。我確定這個問題不在servlet中,所以它在哪裏?

+0

如果你在這裏輸入servlet的絕對URL,會發生什麼:'action =「。」'? –

+0

它應該看起來像「action =」ProjectName/src/org/package/HelloAppIgorServlet.java「嗎? –

+0

不,它應該是您在servlet的'web.xml'中定義的URL映射組件。 –

回答

1

您需要在<form action="." method="post">中設置相應的操作。該操作是您通過中的<servlet-mapping>定義的servlet的(相對)URL。