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;
import com.sun.org.apache.xml.internal.serialize.Printer;
/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public LoginServlet() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//super.doGet(request, response);//將此行刪掉
// 获取用户名
String strUserName=new String(request.getParameter("txtUserName").getBytes("ISO8859-1"),"UTF-8");
// 获取密码
String strPassWord=new String(request.getParameter("txtPassword").getBytes("ISO8859-1"),"UTF-8");
//String strPassWord = request.getParameter("txtPassword");
// 對字符在進行設置
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
// 對resquest對象設置
request.setAttribute("strUserName", strUserName);
request.setAttribute("strPassWord", strPassWord);
// 定義一個PrintWriter的對戲
PrintWriter out = response.getWriter();
// 輸入
out.write("<html><body>使用者姓名:"+strUserName+" 密碼是:"+strPassWord+"</body></html>");
// 完成後的後期操作
out.flush();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//super.doPost(request, response);//將此行刪去
// 获取用户名
String strUserName=new String(request.getParameter("txtUserName").getBytes("ISO8859-1"),"UTF-8");
// 获取密码
String strPassWord=new String(request.getParameter("txtPassword").getBytes("ISO8859-1"),"UTF-8");
//String strPassWord = request.getParameter("txtPassword");
// 對字符在進行設置
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
// 對resquest對象設置
request.setAttribute("strUserName", strUserName);
request.setAttribute("strPassWord", strPassWord);
// 定義一個PrintWriter的對戲
PrintWriter out = response.getWriter();
// 輸入
out.write("<html><body>使用者姓名:"+strUserName+" 密碼是:"+strPassWord+"</body></html>");
// 完成後的後期操作
out.flush();
out.close();
}
}
访问时,一直报错:
type: Status report
message: HTTP method GET is not supported by this URL
description: The specified HTTP method is not allowed for the requested resource (HTTP
method GET is not supported by this URL).
经过上网查询,原因如下:
1,继承自HttpServlet的Servlet没有重写对于请求和响应的处理方法:doGet或doPost等方法;默认调
用父类的doGet或doPost等方法;
2,父类HttpServlet的doGet或doPost等方法覆盖了你重写的doGet或doPost等方法;
不管是1或2,父类HttpServlet的doGet或doPost等方法的默认实现是返回状态代码为405的HTTP错误表示
对于指定资源的请求方法不被允许。
解决方法:
1,子类重写doGet或doPost等方法;
2,在你扩展的Servlert中重写doGet或doPost等方法来处理请求和响应时 不要调用父类HttpServlet的
doGet或doPost等方法,即去掉super.doGet(request, response)和super.doPost(request, response);
发表评论