今天学习上午改了一天的bug才发现是个笑话,本来就没错不过也熟悉了idea tomcat的部署也不是一无所获。了解了http的路径的详细流程
1、学习了java web的生命周期
其中1.1构造开始
public HelloServlet()
1.2初始化
public void init(ServletConfig config) throws ServletException
1.3执行service
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
1.4销毁结束public void destroy()
----------------------------------------------------------------------------------------------------------------------
public class HelloServlet implements Servlet {
//生命周期
//构造器方法
public HelloServlet() {
System.out.println("1");
}
//初始化方法
//前两次只会在开始时被调用
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println("2");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
//执行service方法
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("3,hello");
//类型转换,子类才有getMonth方法
HttpServletRequest httpServletRequest= (HttpServletRequest) req;
//获取请求方式
String method=httpServletRequest.getMethod();
System.out.println(method);
if("get".equals(method)) {
doGet();
} else if ("post".equals(method)) {
doPost();
}
}
//上面的get请求操作
public void doGet(){
System.out.println("get请求");
System.out.println("get请求");
System.out.println("get请求");
}
//上面的post请求操作
public void doPost(){
System.out.println("post请求");
System.out.println("post请求");
System.out.println("post请求");
System.out.println("post请求");
}
@Override
public String getServletInfo() {
return null;
}
//销毁方法
@Override
public void destroy() {
System.out.println("4");
}
}
2、学习了请求的分发处理其中有两种一种是get,一种是post
2.1、学会了用子类的强转使用getMethod()方法
HttpServletRequest httpServletRequest= (HttpServletRequest) req;
//获取请求方式 String method=httpServletRequest.getMethod();
2.3、学会了用函数来优化代码
public void doPost(){
System.out.println("post请求");
System.out.println("post请求");
System.out.println("post请求");
System.out.println("post请求"); }
public void doPost(){
}
使用
if("get".equals(method)) {
doGet(); } else if
("post".equals(method)) { doPost();
}
2.4学习了html请求



