Java教程

2021-11-16

本文主要是介绍2021-11-16,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

java web 1
servlet应用 HttpServlte类

Http主要有两大功能。
第一是根据用户请求方式不同,定义相应的do ()方法处理用户请求,例如:
与get请求方式的doGet()方法。与post方式对应的doPost()方法。
第二是通过servlce()方法将HTTP请求和响应分别强转为HttpServletRequest 和HttpServletResponse类型的对象。

是使用JAVA语言编写的运行在服务器端的程序,狭义上是指JAVA语言实现的一个接口;广义上是指任何实现了这个接口的类。主要用于处理客户端发来的http请求,并反应一个响应,能处理的请求有doGet()和doPost()等方法。
HttpServlet:
1、根据用户请求方式的不同,定义相应的doXxx()方法处理请求(例:GET——doGet() POST——doPost() );
2、通过service方法将HTTP请求和响应分别强转为HttpServletRequest和HttpServletResponse类型的对象。

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 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);
}

}

Insert title here 用户名:
密    码:
爱好: 唱歌 跳舞 足球

用户名:
密    码:
爱好: 唱歌 跳舞 足球
这篇关于2021-11-16的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!