标签:根据 uid comm txt over 分享图片 style 载器 hand
1、Servle简介
Servlet是运行在服务端的Java小程序,是Sun公司提供的一套规范(接口),用于处理客户端请求、相应给浏览器的动态资源。
Servlet规范包含三个技术点:
2、Servlet快速入门
步骤:
开发中会使用继承HttpServlet的类:
3、Servlet接口中的方法
4、HttpServlet类的方法
5、Servlet中url-pattern的配置的方式
6、ServletContext对象
ServletContext对象代表web应用上下文对象,ServletContext对象内部封装的是该web应用的信息,一个web应用中只有一个ServletContext对象。
ServletContext对象的获取方式:
ServletContext servletContext = config.getServletContext();
ServletContext servletContext = this.getServletContext();
ServletContext的作用:
获得web应用全局的初始化参数
获得web应用中任何资源的绝对路径
1 package com.alphajuns.context; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletContext; 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 /** 12 * Servlet implementation class ContextServlet 13 */ 14 public class ContextServlet extends HttpServlet { 15 private static final long serialVersionUID = 1L; 16 17 public ContextServlet() { 18 super(); 19 } 20 21 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 22 // 获取ServletContext对象 23 ServletContext servletContext = getServletContext(); 24 // 获取初始化参数 25 String initParameter = servletContext.getInitParameter("driver"); 26 System.out.println(initParameter); 27 // 1、获得a.txt的绝对路径 28 String realPath_a = servletContext.getRealPath("a.txt"); 29 System.out.println(realPath_a); 30 // 2、获得b.txt的绝对路径 31 String realPath_b = servletContext.getRealPath("WEB-INF/b.txt"); 32 System.out.println(realPath_b); 33 // 3、获得c.txt的绝对路径 34 String realPath_c = servletContext.getRealPath("WEB-INF/classes/c.txt"); 35 System.out.println(realPath_c); 36 37 // 获取在src下的资源可以通过类加载器的方式 38 String path = ContextServlet.class.getClassLoader().getResource("c.txt").getPath(); 39 System.out.println(path); 40 } 41 42 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 43 // TODO Auto-generated method stub 44 doGet(request, response); 45 } 46 47 }
ServletContext是一个域对象
常用方法:
setAttribute(String name, Object obj);
getAttribute(String name);
removeAttribute(String name);
登录应用举例:判断用户名和密码是否正确,并记录正确登录的次数
1 package com.alphajuns.login; 2 3 import java.io.IOException; 4 import java.sql.SQLException; 5 6 import javax.servlet.ServletContext; 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import org.apache.commons.dbutils.QueryRunner; 13 import org.apache.commons.dbutils.handlers.BeanHandler; 14 15 import com.alphajuns.domain.User; 16 import com.alphajuns.utils.DataSourceUtils; 17 18 public class LoginServlet extends HttpServlet { 19 20 @Override 21 public void init() throws ServletException { 22 int count = 0; 23 this.getServletContext().setAttribute("count", count); 24 } 25 26 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 27 // 获得用户名和密码 28 String username = request.getParameter("username"); 29 String password = request.getParameter("password"); 30 31 // 从数据库获取用户名和密码 32 // 获取数据库执行对象 33 QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource()); 34 String sql = "select * from user where username=? and password=?"; 35 // 对查询结果进行封装 36 User user = null; 37 try { 38 user = qr.query(sql, new BeanHandler<User>(User.class), username, password); 39 } catch (SQLException e) { 40 e.printStackTrace(); 41 } 42 43 // 根据查询结果判断登录状态 44 if (user != null) { 45 // 从ServletContext中取出count进行计算++ 46 ServletContext servletContext = this.getServletContext(); 47 Integer count = (Integer) servletContext.getAttribute("count"); 48 count++; 49 response.getWriter().write(user.toString() + "login successful" + count); 50 // 把登录次数存入ServletContext 51 servletContext.setAttribute("count", count); 52 } else { 53 response.getWriter().write("your username or password is wrong"); 54 } 55 } 56 57 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 58 doGet(request, response); 59 } 60 }
标签:根据 uid comm txt over 分享图片 style 载器 hand
原文地址:https://www.cnblogs.com/alphajuns/p/9936463.html