Servlet笔记
1 Servlet的快速入门
1 web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>WEBTest13</display-name> <servlet> <servlet-name>myservlet</servlet-name> <servlet-class>p1.servlet.MyFirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>myservlet</servlet-name> <url-pattern>/fist</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
2 servlet代码
package p1.servlet; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletResponse; /* * 1 创建类实现servlet接口 * 2 添加里面没有实现的方法,并重写service方法 * 3 配置web.xml文件。 */ public class MyFirstServlet implements Servlet{ /** * 重写service方法 */ @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { HttpServletResponse response = (HttpServletResponse) res; res.getWriter().write("this is my first servlet"); } @Override public void init(ServletConfig config) throws ServletException { } @Override public ServletConfig getServletConfig() { return null; } @Override public String getServletInfo() { return null; } @Override public void destroy() { } }
2 Servlet的生命周期
3 Servlet的配置
3 HttpServlet
package p1.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().write(""); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
4 servletContext 概念
5 servletContext 作用
1获取web应用全局的初始化参数
1)web.xml配置
<!-- 配置全局的初始化参数 --> <context-param> <param-name>driver</param-name> <param-value>com.jdbc.mysql.Driver</param-value> </context-param>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>WEBTest13</display-name> <!-- 配置全局的初始化参数 --> <context-param> <param-name>driver</param-name> <param-value>com.jdbc.mysql.Driver</param-value> </context-param> <servlet> <servlet-name>myservlet</servlet-name> <servlet-class>p1.servlet.MyFirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>myservlet</servlet-name> <url-pattern>/first</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>1.html</welcome-file> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>MyServlet</display-name> <servlet-name>MyServlet</servlet-name> <servlet-class>p1.servlet.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/servlet</url-pattern> </servlet-mapping> </web-app>
2)Servlet代码
package p1.servlet; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取ServletContext对象 ServletContext context = this.getServletContext(); //获得初始化参数 String initParameter = context.getInitParameter("driver"); response.getWriter().write(initParameter); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
2 获取web应用中任何资源的绝对路径
//1 获取ServletContext对象 ServletContext context = getServletContext(); //2 Returns the context path of the web application. String contextPath = context.getContextPath();// /WEBTest13 response.getWriter().write(contextPath); // 3 获取绝对路径 getRealPath("1.html")地址相对于web应用 String path = context.getRealPath("1.html"); response.getWriter().write(path);//D:\apache-tomcat-7.0.52\webapps\WEBTest13\1.html
3 ServletContext是一个域对象
6 案例记录成功登录系统的人数
package p2.login_servlet; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import p1.utils.C3P0Utils; import p3.domain.User; public class LoginServlet extends HttpServlet { @Override public void init() throws ServletException { //在Seveltcontext域中存一个数据count int count = 0; this.getServletContext().setAttribute("count", count); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1 获取用户名和密码 String username = request.getParameter("username"); String password = request.getParameter("password"); // 2查询数据库中是否有相应的用户名和密码 QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); String sql = "select * from user where username=? and upassword=?"; Object[] params = {username,password}; User user = null; try { user = qr.query(sql, new BeanHandler<User>(User.class), params); } catch (SQLException e) { e.printStackTrace(); } // 3 根据返回结果给用户不同的显示信息 if(user!=null){ //从servletcontext中取出count进行++运算 ServletContext context = this.getServletContext(); Integer count = (Integer) context.getAttribute("count"); count++; response.getWriter().write(user.toString()); response.getWriter().write(user.getUsername()+":success"+count); }else{ response.getWriter().write("sorry"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }