Servlet:
1.创建一个类实现servlet接口覆盖以下方法:
package com.javaweb.java;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class MyServlet implements Servlet{
public MyServlet(){
System.out.println("myservlet");
}
@Override
public void destroy() {
System.out.println("destroy");
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void init(ServletConfig ServletConfig) throws ServletException {
System.out.println("init");
}
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
System.out.println("service");
}
}
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">
<!-- 配置servlet -->
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.javaweb.java.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
3.访问hello
4.<load-on-startup>可以规定servlet被创建的时机,0最优先
5.通配符:/*和*.html
6.servletConfig:封装了servlet的配置信息并且可以获取servletContext对象
7.获取初始化参数:
<init-param>
<param-name>user</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>123456</param-value>
</init-param>
8.获取初始化参数值:
String user=ServletConfig.getInitParameter("user");
System.out.println("user"+user);
Enumeration<String> names=ServletConfig.getInitParameterNames();
while(names.hasMoreElements()){
String name=names.nextElement();
String value=ServletConfig.getInitParameter(name);
System.out.println(name+"="+value);
}
重点:servletContext:
1)可以由servletconfig获取
2)配置当前web应用的初始化参数
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>jdbcUrl</param-name>
<param-value>jdbc:mysql</param-value>
</context-param>
ServletContext servletContext=ServletConfig.getServletContext();
String driver=servletContext.getInitParameter("driver");
System.out.println("driver:"+driver);
Enumeration<String> names2=servletContext.getInitParameterNames();
while(names2.hasMoreElements()){
String name2=names2.nextElement();
System.out.println("--"+name2);
}
3)getRealPath(string path)获取当前web应用的某一个文件在服务器上的绝对路径
4) getContextPath() 获取当前web应用的名称
1.创建一个类实现servlet接口覆盖以下方法:
package com.javaweb.java;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class MyServlet implements Servlet{
public MyServlet(){
System.out.println("myservlet");
}
@Override
public void destroy() {
System.out.println("destroy");
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void init(ServletConfig ServletConfig) throws ServletException {
System.out.println("init");
}
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
System.out.println("service");
}
}
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">
<!-- 配置servlet -->
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.javaweb.java.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
3.访问hello
4.<load-on-startup>可以规定servlet被创建的时机,0最优先
5.通配符:/*和*.html
6.servletConfig:封装了servlet的配置信息并且可以获取servletContext对象
7.获取初始化参数:
<init-param>
<param-name>user</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>123456</param-value>
</init-param>
8.获取初始化参数值:
String user=ServletConfig.getInitParameter("user");
System.out.println("user"+user);
Enumeration<String> names=ServletConfig.getInitParameterNames();
while(names.hasMoreElements()){
String name=names.nextElement();
String value=ServletConfig.getInitParameter(name);
System.out.println(name+"="+value);
}
重点:servletContext:
1)可以由servletconfig获取
2)配置当前web应用的初始化参数
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>jdbcUrl</param-name>
<param-value>jdbc:mysql</param-value>
</context-param>
ServletContext servletContext=ServletConfig.getServletContext();
String driver=servletContext.getInitParameter("driver");
System.out.println("driver:"+driver);
Enumeration<String> names2=servletContext.getInitParameterNames();
while(names2.hasMoreElements()){
String name2=names2.nextElement();
System.out.println("--"+name2);
}
3)getRealPath(string path)获取当前web应用的某一个文件在服务器上的绝对路径
4) getContextPath() 获取当前web应用的名称