import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet
{
private void sendLoginPage (HttpServletResponse res, HttpServletRequest req, boolean error)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter o = res.getWriter();
o.println("<html><head><title>Sample Login Page</title></head><body>Welcome to Login Page : ");
if (error)
o.println("<fieldset><legend>Login Form : </legend>");
o.println("Login Failed, Please Try Again");
o.println("<form method="+"post"+">");
o.println("<br/><input type="+"text"+"value="+"username"+"/>");
o.println("<br/><input type="+"password"+"value="+""+"/>");
o.println("<br/><input type="+"button"+"value="+"Submit"+"/>");
o.println("</form></fieldset></body></html>");
}
public void doGet (HttpServletResponse res, HttpServletRequest req)
throws ServletException,IOException
{
sendLoginPage ( res, null, false ) ;
}
public void doPost (HttpServletResponse res, HttpServletRequest req)
throws ServletException,IOException
{
String username = req.getParameter("username");
String password = req.getParameter("password");
if ( username.equals("*******") && password.equals("*******") )
{
res.sendRedirect ("http://localhost:7001/ten/r1");
}
else
{
sendLoginPage ( res, null, true ) ;
}
}
}
Run Code Online (Sandbox Code Playgroud)
那么这个Servlet编译好没有任何错误,也可以在服务器上部署,但在尝试通过url访问时显示此错误:
此URL不支持HTTP方法GET
您的方法上反转了HttpServletResponse和HttpServletRequest参数.
旁注:在重写方法上使用@Override注释会导致编译错误(假设您至少使用Java 1.5)
| 归档时间: |
|
| 查看次数: |
2237 次 |
| 最近记录: |