1.什么是Servlet
Servlet是遵循Java Servlet规范的Java类,和一般Java类不同的是,它具有web服务功能,可以处理浏览器的请求并做出响应
Servlet通常用于在服务器端完成访问数据库、调用JavaBean等业务性操作
JSP是基于Servlet开发的,jsp底层还是Servlet来执行的
2.案例一分析
我们来新建一个Servlet:HelloServlet.java
package testServlet;
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;
public class HelloServlet extends HttpServlet {
public HelloServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void init() throws ServletException {
}
}
从配置文件web.xml中,我们知道,<servlet-mapping></servlet-mapping>中指明访问url对应的servlet-name,从<servlet></servlet>查到servlet-name对应的servlet所在位置
你可以按照自己的需求去该这个配置文件
对于浏览器的get和post请求,你可以分别在文件中的doGet和doPost中进行处理响应
3.案例二分析
在servletTest.jsp页面中输入一个用户名字,然后提交给HelloServlet,在页面上输出:“***,你好!欢迎使用servlet”这个字符串。
servletTest.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML>
<html>
<head></head>
<body>
<form method="get" action="./HelloServlet">
请输入姓名:<input type="text" name="name" size="20"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
HelloServlet.java
package testServlet;
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;
public class HelloServlet extends HttpServlet {
public HelloServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(name+",你好!欢迎使用servlet");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void init() throws ServletException {
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>testServlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4.Servlet的生命周期
Servlet的生命周期从Web服务器启动运行时开始,以后会不断处理来自浏览器的访问请求,并将响应结果通过Web服务器返回给客户端,直到Web服务器停止运行,Servet才会被清除。
Servlet API包含于两个包中,即javax.servlet和javax.servlet.http
主要Servlet API介绍
① HttpServlet类是Servlet容器中最重要的一个类,其主要功能是处理Servlet请求和回应处理结果
② HttpServletRequest接口继承自ServletRequest接口,ServletRequest接口中定义了一些获取请求信息的方法
③ HttpServletResponse接口继承自ServletResponse接口,ServletResponse接口可以发送MIME编码数据到客户端,服务器再Servlet程序初始化以后,会创建ServletResponse接口对象,作为参数传递给service()方法。
④ ServletContext接口,每个Web应用只有一个Servletcontext实例(Servlet的环境对象),通过此接口实例可以访问Web应用的所有资源,也可以用于不同Servlet间的数据共享。
获取服务器上的项目路径(物理路径)
ServletContext context = getServletContext();
out.println(context.getRealPath("/"));