import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.jboss.weld.context.ApplicationContext;
public class ServletLogin extends HttpServlet{
private String username;
private String password;
/* public void doGet(HttpServletRequest request,HttpServletResponse reponse){
this.username=(String)request.getParameter("username");
this.password=(String)request.getParameter("password");
System.out.println("123");
}
public void doPost(HttpServletRequest request,HttpServletResponse reponse){
this.username=(String)request.getParameter("username");
this.password=(String)request.getParameter("password");
System.out.println("456");
}*/
public void init(){
//四个域对象(servletcontext ,Session,Request,page)
ServletConfig application=this.getServletConfig();//此对象获得局部变量
ServletContext application1=this.getServletContext();//此对象获得全局变量
String username=application.getInitParameter("username");
String password=application.getInitParameter("password");
String var=application1.getInitParameter("var");
String var1=application.getInitParameter("var");
System.out.println(var);
System.out.println(var1);
System.out.println(password);
System.out.println(username);
System.out.println("789");
// if(this.username.equals(username)&&this.password.equals(password))
//request.sendRedirect("MyJsp.jsp");
}
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.jboss.weld.context.ApplicationContext;
public class ServletLogin extends HttpServlet{
private String username;
private String password;
/* public void doGet(HttpServletRequest request,HttpServletResponse reponse){
this.username=(String)request.getParameter("username");
this.password=(String)request.getParameter("password");
System.out.println("123");
}
public void doPost(HttpServletRequest request,HttpServletResponse reponse){
this.username=(String)request.getParameter("username");
this.password=(String)request.getParameter("password");
System.out.println("456");
}*/
public void init(){
//四个域对象(servletcontext ,Session,Request,page)
ServletConfig application=this.getServletConfig();//此对象获得局部变量
ServletContext application1=this.getServletContext();//此对象获得全局变量
String username=application.getInitParameter("username");
String password=application.getInitParameter("password");
String var=application1.getInitParameter("var");
String var1=application.getInitParameter("var");
System.out.println(var);
System.out.println(var1);
System.out.println(password);
System.out.println(username);
System.out.println("789");
// if(this.username.equals(username)&&this.password.equals(password))
//request.sendRedirect("MyJsp.jsp");
}
}
代码当我把doGet()方法注释掉后就会出现这种情况,原因是doGet()/doPost()方法主要用于处理表单提交过来的数据,在表单<form></form>中method属性由于默认的是get方式提交也就是调用doGet()方法,当是method是POST方式提交时就是调用doPost()方法。即使我们没有编写jsp,我们直接用URL访问servlet时发送的时Http请求,servlet默认访问doGet方法,但是我们程序中没有重写父类的doGet()方法,而父类的doGet()方法又不支持这种URL方式,所以出现HTTP method GET is not supported by this URL这种错误。只需要重写doGet方法就可以。
当我们用JSP编写表单提交数据到servlet时如果我们<form>中的属性method是设置了doPOST/doGet方法,但是我们servlet还是没有重写doPOST/doGet方法还是会出现一样的问题。如下图(这里就举没有重写doPOST的形式)
还有一种情况也会出现相同的情况就是包的路径错误导致的。如下图(只需要把的有红叉包remove即可)