一:第一个jsp程序
1.项目设计结构
2.新建Person.java
package com.java.demo;
public class Person {
public void printStr() {
System.out.println("show info-----");
}
}
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 应用的名称
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);
//getContextPath
String contextPath=servletContext.getContextPath();
System.out.println(contextPath); } @Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
System.out.println("service"); }
}
7.运行效果
8.获取当前 WEB 应用的某一个文件对应的输入流
对应两种方式
package pottt; import java.io.IOException;
import java.io.InputStream;
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);
//getContextPath
String contextPath=servletContext.getContextPath();
System.out.println(contextPath);
//
try {
ClassLoader classLoader=getClass().getClassLoader();
InputStream in=classLoader.getResourceAsStream("jdbc.properties");
System.out.println(in);
} catch (Exception e) {
e.printStackTrace();
}
//
try {
InputStream in2=servletContext.getResourceAsStream("/WEB-INF/classes/jdbc.properties");
System.out.println(in2);
} catch (Exception e) {
e.printStackTrace();
} } @Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
System.out.println("service"); }
}
9.效果
五:ServletRequest
1.主要获取参数的方法
> String getParameter(String name): 根据请求参数的名字, 返回参数值.
若请求参数有多个值(例如 checkbox), 该方法只能获取到第一个提交的值.
> String[] getParameterValues(String name): 根据请求参数的名字, 返回请求参数对应的字符串数组.
> Enumeration getParameterNames(): 返回参数名对应的 Enumeration 对象,
类似于 ServletConfig(或 ServletContext) 的 getInitParameterNames() 方法.
> Map getParameterMap(): 返回请求参数的键值对: key: 参数名, value: 参数值, String 数组类型.
2.程序