Javaweb2

wuchangjian2021-11-16 17:32:03编程学习

第三章:Servlet、ServletConfig、ServletContext
1.Servlet技术
1.1 Servlet概述:
什么是servlet:它是运行在web服务器端的一个java小程序,简单来说,它就是一个java类
在使用servlet时,需要导入javax.servlet下的jar包。比如:tomcat服务器提供了
servlet的servlet-api.jar包,所以在开发时用tomcat服务器就不需要再导入servlet的jar包。
Servlet它是一个资源,是用来被用户访问的资源,同时是一个动态资源,它是由sun指定的servlet规范。
Servlet的作用:它是基于Http协议的,主要是用来接收客户端发送的请求,处理请求,把数据响应给客户端。

package cn.itcast.servlet;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class servlet02 {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		ServletConfig config=this.getServletconfig();
		String name=config.getServletName();
		System.out.println("servlet name:"+name);
		
		Enumeration<String> er =config.getInitParameterNames();
		while(er.hasMoreElements()) {
			String name1=er.nextElement();
			String valule2=config.getInitParameter(name1);
			
			System.out.println("全局初始化参数名称:"+name1);
			System.out.println("全局初始化参数的值:"+valule2);
		}
		System.out.println("————————+——————————————");
		
		ServletContext context=this.getServletContext();
		
		Enumeration<String> er1=context.getInitParameterNames();
		while(er.hasMoreElements()) {
			String name1=er1.nextElement();
			String valule2=context.getInitParameter(name1);
			
			System.out.println("全局初始化参数名称:"+name1);
			System.out.println("全局初始化参数的值:"+valule2);
	}
}

	private ServletConfig getServletconfig() {
		// TODO Auto-generated method stub
		return null;
	}

	private ServletContext getServletContext() {
		// TODO Auto-generated method stub
		return null;
	}

	
}

package cn.itcast.servlet;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class servlet03 extends httpservlet{
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		ServletContext context=this.getServletContext();
		Enumeration<String> er =context.getInitParameterNames();
		while(er.hasMoreElements()) {
			String name1=er.nextElement();
			String valule2=context.getInitParameter(name1);
			
			System.out.println("全局初始化参数名称:"+name1);
			System.out.println("全局初始化参数的值:"+valule2);
		}
	}

	protected void dopost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		doGet(request,response);
	}
}

``

package cn.itcast.servlet;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class servlet03 extends httpservlet{
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		ServletContext context=this.getServletContext();
		Enumeration<String> er =context.getInitParameterNames();
		while(er.hasMoreElements()) {
			String name1=er.nextElement();
			String valule2=context.getInitParameter(name1);
			
			System.out.println("全局初始化参数名称:"+name1);
			System.out.println("全局初始化参数的值:"+valule2);
		}
	}

	protected void dopost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		doGet(request,response);
	}
}


package cn.itcast.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class httpservlet  extends javax.servlet.http.HttpServlet{
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		System.out.println("收到前端的get请求");
	}
	protected void dopost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		doGet(request,response);
	}
 
}


<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
 	<form action="/HttpServlet/httpservlet" method="GET">
 	 	用户名:<input type="text" name="username"><br>
 	 	密  &nbsp;&nbsp;&nbsp;码:<input type="password" name="password"><br>
 	 	爱好:
 	 	<input type="checkbox" name="hobby" value="sing">唱歌
 	 	<input type="checkbox" name="hobby" value="dance">跳舞
 	 	<input type="checkbox" name="hobby" value="football">足球<br>
 	 	<input type="submit" value="get提交">
 	</form>
 	<hr/>
 		<form action="/HttpServlet/HttpServlet" method="POST">
 	 	用户名:<input type="text" name="username"><br>
 	 	密  &nbsp;&nbsp;&nbsp;码:<input type="password" name="password"><br>
 	 	爱好:
 	 	<input type="checkbox" name="hobby" value="sing">唱歌
 	 	<input type="checkbox" name="hobby" value="dance">跳舞
 	 	<input type="checkbox" name="hobby" value="football">足球<br>
 	 	<input type="submit" value="post提交">
 	</form>
</body>
</html>

在这里插入图片描述

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。