我是servlet / jsp / portlet tecnology的新手,我正在努力理解,所以我说这个servlet有问题:
package trekking;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Login
*/
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* see HttpServlet#HttpServlet()
*/
public Login() {
}
/**
* @param name
* @param pass
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response, String name, String pass) throws ServletException, IOException {
String u,p;
u=request.getParameter(name);
p=request.getParameter(pass);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("You are "+u+"!");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}
}
通过以下形式在html页面中调用它:
<form action="Login" method="post">
<label for="uname">UserName</label>
<input name="user" id="uname" type="text"></input>
<label for="pwd">Password</label>
<input name="pass" id="pwd" type="password"></input>
<input type="submit" value="invia">
</form>
如果你还需要别的东西,我就在这里。 谢谢。 :d
答案 0 :(得分:2)
您尚未覆盖doPost
方法。你已经超负荷了。
protected void doPost(HttpServletRequest request, HttpServletResponse response, String name, String pass) throws ServletException, IOException {
由于没有任何东西正在调用您的重载方法,因此您将获得默认的继承HttpServlet#doPost()
行为,返回405.
答案 1 :(得分:0)
从doPost方法中删除'name'和'pass'参数。