一:第一个jsp程序
1.项目设计结构
2.新建Person.java
1 package com.java.demo; 2 public class Person { 3 public void printStr() { 4 System.out.println("show info-----"); 5 } 6 }
3.书写index.jsp
<%@page import="com.java.demo.Person"%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>======</h1> <% Person p=new Person(); p.printStr(); %> </body> </html>
4.运行效果
二:第一个servlet程序以及知识点
1.结构
2.新建程序
package pottt; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /** * 需要实现Servlet接口 * @author caojun * */ public class rertrtrt implements Servlet{ @Override public void destroy() { System.out.println("destroy"); } @Override public ServletConfig getServletConfig() { System.out.println("getServletConfig"); return null; } @Override public String getServletInfo() { System.out.println("getServletInfo"); return null; } @Override public void init(ServletConfig arg0) throws ServletException { System.out.println("init"); } @Override public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { System.out.println("service"); } }
3.web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <servlet> <servlet-name>hello</servlet-name> <servlet-class>pottt.rertrtrt</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/he</url-pattern> </servlet-mapping> </web-app>
4.访问
5.映射
在 web.xml 文件中配置和映射这个 Servlet
<!-- 配置和映射 Servlet -->
<servlet>
<!-- Servlet 注册的名字 -->
<servlet-name>helloServlet</servlet-name>
<!-- Servlet 的全类名 -->
<servlet-class>com.atguigu.javaweb.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<!-- 需要和某一个 servlet 节点的 serlvet-name 子节点的文本节点一致 -->
<servlet-name>helloServlet</servlet-name>
<!-- 映射具体的访问路径: / 代表当前 WEB 应用的根目录. -->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
6.生命周期的四个函数
Servlet 生命周期的方法: 以下方法都是由 Serlvet 容器负责调用.
1). 构造器: 只被调用一次. 只有第一次请求 Servlet 时, 创建 Servlet 的实例. 调用构造器.
这说明 Serlvet 的单实例的!
2). init 方法: 只被调用一次. 在创建好实例后立即被调用. 用于初始化当前 Servlet.
3). service: 被多次调用. 每次请求都会调用 service 方法. 实际用于响应请求的.
4). destroy: 只被调用一次. 在当前 Servlet 所在的 WEB 应用被卸载前调用. 用于释放当前 Servlet 所占用的资源.
7.load-on-startup 参数(在加载时的顺序配置)
1). 配置在 servlet 节点中:
<servlet>
<!-- Servlet 注册的名字 -->
<servlet-name>secondServlet</servlet-name>
<!-- Servlet 的全类名 -->
<servlet-class>com.atguigu.javaweb.SecondServlet</servlet-class>
<!-- 可以指定 Servlet 被创建的时机 -->
<load-on-startup>2</load-on-startup>
</servlet>
2). load-on-startup: 可以指定 Serlvet 被创建的时机.
若为负数, 则在第一次请求时被创建.
若为 0 或正数, 则在当前 WEB 应用被Serlvet 容器加载时创建实例, 且数组越小越早被创建.
8.serlvet-mapping参数
1). 同一个Servlet可以被映射到多个URL上,即多个 <servlet-mapping> 元素的<servlet-name>子元素的设置值可以是同一个Servlet的注册名。
2). 在Servlet映射到的URL中也可以使用 * 通配符,但是只能有两种固定的格式:一种格式是“*.扩展名”,另一种格式是以正斜杠(/)开头并以“/*”结尾。
<servlet-mapping>
<servlet-name>secondServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
OR
<servlet-mapping>
<servlet-name>secondServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
三:ServletConfig变量
1.在web.xml中配置初始化参数
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <servlet> <servlet-name>hello</servlet-name> <servlet-class>pottt.rertrtrt</servlet-class> <init-param> <param-name>user</param-name> <param-value>caojun</param-value> </init-param> <init-param> <param-name>password</param-name> <param-value>123321</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/he</url-pattern> </servlet-mapping> </web-app>
2.使用函数获取
package pottt; import java.io.IOException; import java.util.Enumeration; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /** * 需要实现Servlet接口 * @author caojun * */ public class rertrtrt implements Servlet{ public rertrtrt() { System.out.println("构造函数"); } @Override public void destroy() { System.out.println("destroy"); } @Override public ServletConfig getServletConfig() { System.out.println("getServletConfig"); return null; } @Override public String getServletInfo() { System.out.println("getServletInfo"); return null; } @Override public void init(ServletConfig servletConfig) throws ServletException { //getInitParameter String user=servletConfig.getInitParameter("user"); System.out.println("user="+user); //getInitParameterNames Enumeration<String> enu=servletConfig.getInitParameterNames(); while(enu.hasMoreElements()) { String name=enu.nextElement(); String value=servletConfig.getInitParameter(name); System.out.println("name="+name+",value="+value); } } @Override public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { System.out.println("service"); } }
3.运行效果
四:ServletContext
0.框架
1.获取
可以由 SerlvetConfig 获取:
ServletContext servletContext = servletConfig.getServletContext();
2.配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <!-- 配置当前 WEB 应用的初始化参数 --> <context-param> <param-name>driver</param-name> <param-value>com.mysql.jdbc.Driver</param-value> </context-param> <context-param> <param-name>url</param-name> <param-value>sun.java.org</param-value> </context-param> <servlet> <servlet-name>hello</servlet-name> <servlet-class>pottt.rertrtrt</servlet-class> <init-param> <param-name>user</param-name> <param-value>caojun</param-value> </init-param> <init-param> <param-name>password</param-name> <param-value>123321</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/he</url-pattern> </servlet-mapping> </web-app>
3.获取当前的web应用的参数
使用的函数与上面的获取servlet的初始化参数的方法相同
ServletContext servletContext = servletConfig.getServletContext();
String driver = servletContext.getInitParameter("driver");
System.out.println("driver:" + driver);
Enumeration<String> names2 = servletContext.getInitParameterNames();
while(names2.hasMoreElements()){
String name = names2.nextElement();
System.out.println("-->" + name);
}
4.获取当前 WEB 应用的某一个文件在服务器上的绝对路径
package pottt; import java.io.IOException; import java.util.Enumeration; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /** * 需要实现Servlet接口 * @author caojun * */ public class rertrtrt implements Servlet{ public rertrtrt() { System.out.println("构造函数"); } @Override public void destroy() { System.out.println("destroy"); } @Override public ServletConfig getServletConfig() { System.out.println("getServletConfig"); return null; } @Override public String getServletInfo() { System.out.println("getServletInfo"); return null; } @Override public void init(ServletConfig servletConfig) throws ServletException { //getInitParameter String user=servletConfig.getInitParameter("user"); System.out.println("user="+user); //getInitParameterNames Enumeration<String> enu=servletConfig.getInitParameterNames(); while(enu.hasMoreElements()) { String name=enu.nextElement(); String value=servletConfig.getInitParameter(name); System.out.println("name="+name+",value="+value); } //getRealPath ServletContext servletContext=servletConfig.getServletContext(); String path=servletContext.getRealPath("/note.txt"); System.out.println(path); } @Override public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { System.out.println("service"); } }
5.运行效果
6.获取当前 WEB 应用的名称
1 package pottt; 2 3 import java.io.IOException; 4 import java.util.Enumeration; 5 6 import javax.servlet.Servlet; 7 import javax.servlet.ServletConfig; 8 import javax.servlet.ServletContext; 9 import javax.servlet.ServletException; 10 import javax.servlet.ServletRequest; 11 import javax.servlet.ServletResponse; 12 /** 13 * 需要实现Servlet接口 14 * @author caojun 15 * 16 */ 17 public class rertrtrt implements Servlet{ 18 19 public rertrtrt() { 20 System.out.println("构造函数"); 21 } 22 @Override 23 public void destroy() { 24 System.out.println("destroy"); 25 26 } 27 28 @Override 29 public ServletConfig getServletConfig() { 30 System.out.println("getServletConfig"); 31 return null; 32 } 33 34 @Override 35 public String getServletInfo() { 36 System.out.println("getServletInfo"); 37 return null; 38 } 39 40 @Override 41 public void init(ServletConfig servletConfig) throws ServletException { 42 //getInitParameter 43 String user=servletConfig.getInitParameter("user"); 44 System.out.println("user="+user); 45 //getInitParameterNames 46 Enumeration<String> enu=servletConfig.getInitParameterNames(); 47 while(enu.hasMoreElements()) { 48 String name=enu.nextElement(); 49 String value=servletConfig.getInitParameter(name); 50 System.out.println("name="+name+",value="+value); 51 } 52 //getRealPath 53 ServletContext servletContext=servletConfig.getServletContext(); 54 String path=servletContext.getRealPath("/note.txt"); 55 System.out.println(path); 56 //getContextPath 57 String contextPath=servletContext.getContextPath(); 58 System.out.println(contextPath); 59 60 } 61 62 @Override 63 public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { 64 System.out.println("service"); 65 66 } 67 }
7.运行效果
8.获取当前 WEB 应用的某一个文件对应的输入流
对应两种方式
1 package pottt; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.Enumeration; 6 7 import javax.servlet.Servlet; 8 import javax.servlet.ServletConfig; 9 import javax.servlet.ServletContext; 10 import javax.servlet.ServletException; 11 import javax.servlet.ServletRequest; 12 import javax.servlet.ServletResponse; 13 /** 14 * 需要实现Servlet接口 15 * @author caojun 16 * 17 */ 18 public class rertrtrt implements Servlet{ 19 20 public rertrtrt() { 21 System.out.println("构造函数"); 22 } 23 @Override 24 public void destroy() { 25 System.out.println("destroy"); 26 27 } 28 29 @Override 30 public ServletConfig getServletConfig() { 31 System.out.println("getServletConfig"); 32 return null; 33 } 34 35 @Override 36 public String getServletInfo() { 37 System.out.println("getServletInfo"); 38 return null; 39 } 40 41 @Override 42 public void init(ServletConfig servletConfig) throws ServletException { 43 //getInitParameter 44 String user=servletConfig.getInitParameter("user"); 45 System.out.println("user="+user); 46 //getInitParameterNames 47 Enumeration<String> enu=servletConfig.getInitParameterNames(); 48 while(enu.hasMoreElements()) { 49 String name=enu.nextElement(); 50 String value=servletConfig.getInitParameter(name); 51 System.out.println("name="+name+",value="+value); 52 } 53 //getRealPath 54 ServletContext servletContext=servletConfig.getServletContext(); 55 String path=servletContext.getRealPath("/note.txt"); 56 System.out.println(path); 57 //getContextPath 58 String contextPath=servletContext.getContextPath(); 59 System.out.println(contextPath); 60 // 61 try { 62 ClassLoader classLoader=getClass().getClassLoader(); 63 InputStream in=classLoader.getResourceAsStream("jdbc.properties"); 64 System.out.println(in); 65 } catch (Exception e) { 66 e.printStackTrace(); 67 } 68 // 69 try { 70 InputStream in2=servletContext.getResourceAsStream("/WEB-INF/classes/jdbc.properties"); 71 System.out.println(in2); 72 } catch (Exception e) { 73 e.printStackTrace(); 74 } 75 76 } 77 78 @Override 79 public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { 80 System.out.println("service"); 81 82 } 83 }
9.效果
五:ServletRequest
1.主要获取参数的方法
> String getParameter(String name): 根据请求参数的名字, 返回参数值.
若请求参数有多个值(例如 checkbox), 该方法只能获取到第一个提交的值.
> String[] getParameterValues(String name): 根据请求参数的名字, 返回请求参数对应的字符串数组.
> Enumeration getParameterNames(): 返回参数名对应的 Enumeration 对象,
类似于 ServletConfig(或 ServletContext) 的 getInitParameterNames() 方法.
> Map getParameterMap(): 返回请求参数的键值对: key: 参数名, value: 参数值, String 数组类型.
2.程序