【servlet与Web.xml配置】

【关联01:处理类与请求的关联】

web.xml配置为:

  <!--  定义如何在浏览器中访问此serlvet 如下即:通过/abc可以访问到demo类去处理-->	
  <servlet-mapping>
  	<servlet-name>demo</servlet-name>
  	<url-pattern>*.gdy</url-pattern>
  </servlet-mapping>
  <servlet>
  	<servlet-name>GenericServletone</servlet-name>
  	<servlet-class>ServerByGenerServlet</servlet-class>
	<!-- 启动时加载,即服务器启动时就加载该类,数组代表启动时,启动的顺序 -->
  	<load-on-startup>3</load-on-startup>
  </servlet>
java对应的类代码为:

 public class GServletConfig extends HttpServlet {
	public void service(ServletRequest req, ServletResponse res)
			throws ServletException, IOException {
	}
 }

【关联02:通过ServletConfig获取配置信息】

web.xml配置为:

  <!-- GServletConig处理类 -->
  <servlet>
  	<servlet-name>Gconfig</servlet-name>
  	<servlet-class>GServletConfig</servlet-class>
  	<!-- 定义类的全局变量 -->
  	<init-param>
  		<param-name>name</param-name>
  		<param-value>你猜猜我是谁</param-value>
  	</init-param>
  	<init-param>
  		<param-name>gdy</param-name>
  		<param-value>我是gdy</param-value>
  	</init-param>
  	<load-on-startup>5</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>Gconfig</servlet-name>
  	<url-pattern>*.gan</url-pattern>
  </servlet-mapping>
对应的java获取配置代码为:

/**
 * 处理【*.gan】请求
 * 功能:学习ServletConfig
 * @author Administrator
 * 
 * 获取ServletConfig的方法
 * 		1.采用带参数的init方法
 * 		2.采用servlet实例获取,(但不要写在带参数的init方法里面)
 *
 */
public class GServletConfig extends HttpServlet {
	@Override
	public void service(ServletRequest req, ServletResponse res)
			throws ServletException, IOException {
//		//获取web.xml配置文件中的单个配置信息
//		String name=this.getServletConfig().getInitParameter("name");
//		System.out.println(name);
		
		//获取多个配置信息
		Enumeration<String> enumer = this.getInitParameterNames();
		while (enumer.hasMoreElements()) {
			String wname = (String) enumer.nextElement();
			System.out.println(wname+":"+this.getServletConfig().getInitParameter(wname));
		}
		
	}
}

【关联03:通过ServletContext获取定义的全局变量】

web.xml配置为:

  <!-- 定义应用程序的全局的变量 -->
  <context-param>
  	<param-name>name1</param-name>
  	<param-value>全局标量01</param-value>
  </context-param>
  <context-param>
  	<param-name>name2</param-name>
  	<param-value>全局变量02</param-value>
  </context-param>
  
  <!-- com.gdy.Servlet.GServletContext 处理类 处理请求为*.ggt -->
  <servlet>
  	<servlet-name>Gggt</servlet-name>
  	<servlet-class>com.gdy.Servlet.GServletContext</servlet-class>
  	<load-on-startup>7</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>Gggt</servlet-name>
  	<url-pattern>*.ggt</url-pattern>
  </servlet-mapping>

java类获取方法为:

/**
 * 通过ServletContext获取定义的全局变量
 * @author Administrator
 *
 */
public class GServletContext extends HttpServlet {
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Enumeration<String> genum = this.getServletConfig().getServletContext().getInitParameterNames();
		while (genum.hasMoreElements()) {
			String str = (String) genum.nextElement();
			System.out.println(str + ":" +this.getServletContext().getInitParameter(str));
		}
	}
}

【关联04:请求转发处理】

web.xml代码为:

  <!-- com.gdy.Servlet.GservletRealy类的创建 -->
  <servlet>
  	<servlet-name>Grealy</servlet-name>
  	<servlet-class>com.gdy.Servlet.GservletRealy</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>Grealy</servlet-name>
  	<url-pattern>*.realy</url-pattern>
  </servlet-mapping>
  
  <!-- com.gdy.Servlet.GservletGetRealy类的创建 -->
  <servlet>
  	<servlet-name>Gdd</servlet-name>
  	<servlet-class>com.gdy.Servlet.GservletGetRealy</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>Gdd</servlet-name>
  	<url-pattern>*.gdd</url-pattern>
  </servlet-mapping>
Java请求转发,和转发后处理代码为:
[请求转发类]

/**
 * 请求转发实例,
 * 处理请求为【*.realy】
 * 功能:将带*。realy的请求转发到*。getregly上处理
 *
 */
public class GservletRealy extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//获取全局变量
		ServletContext context = this.getServletContext();
		//添加值
		context.setAttribute("age", "13");
		req.setAttribute("gname", "你猜我是什么");
		System.out.println("开始转发");
		//获取请求转发器
		RequestDispatcher rd = context.getRequestDispatcher("/aa.gdd");
		//转发请求
		rd.forward(req, resp);
		
	}
}
[转发后处理类为]
/**
 * 处理请求类为【*.gdd】
 * @author Administrator
 *
 */
public class GservletGetRealy extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("成功接收");
		//获取context传递过来的值
		String contname=(String)this.getServletContext().getAttribute("age");
		//获取servletrequest传递过来的值
		String sername=(String)req.getAttribute("gname");
		System.out.println("context传递过来的值:"+contname);
		System.out.println("servletrequest传递过来的值:"+sername);
	}
}


Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐