一.学习内容
1.Servlet生命周期
2.Servlet应用——HttpServlet类
3.实现Servlet虚拟路径的映射
二.代码如下:
1.
package com.hg;
import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class HelloWorldServlet extends GenericServlet{
@Override
public void init(ServletConfig config)throws ServletException{
System.out.println("init nethod is calling");
}
@Override
public void service(ServletRequest request,ServletResponse response) throws ServletException{
System.out.println("hello world");
}
public void destroy() {
System.out.println("destroy method is called");
}
}
package com.hg;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
public class myHttpServlet extends HttpServlet{
protected void doGet(HttpServletRequest request, ServletResponse response)throws ServletException {
String method=request.getMethod();
System.out.println(method);
System.out.println("Get方法");
}
protected void doPost(HttpServletRequest request, ServletResponse response)throws ServletException {
String method=request.getMethod();
System.out.println(method);
System.out.println("post方法");
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/Deo10.9/myHttpServlet" method="post">
姓名<input type="text"name="name"/><br/>
密码<input type="text"name="psw"/><br/>
<input type="submit" value="提交">
</form>
</body>
</html>