servlet基本配置(2)
阿新 • • 發佈:2019-02-03
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應用的名稱