⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servletexample.java

📁 Java程序设计实验与实训源代码经典的JAVA学习教材
💻 JAVA
字号:
//Servlet小程序结构示意
import Javax.servlet.*;//从Java扩展包中导入标准的Servlet API
import Javax.servlet.http.*;//基于HTTP协议,从扩展包中导入HTTP API
import Java.io.*;//一般需要通过Writer或OutputStream向响应中写入信息,所以导入Java中的IO API

public class ServletDemo extends HttpServlet{//用HttpServlet来专门处理HTTP通信协议
	public void init(ServletConfig config) throws ServletException{
		super.init(config);
		//在Servlet运行之前进行一些初始化的设置
	}
	
	public  void  service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{//Servlet程序默认的入口方法
		String clientHostName=req.getRemoteHost();
		String clientIPAddress=req.getRemoteAddr();
		//使用HttpServletRequest对象req来读取与客户端请求相关的信息
		
		res.setContentType("text/html");
		//通过HttpServletResponse对象res来设置一些与发往客户端响应相关的信息
		
		PrintWriter out=res.getWriter();//取得一个PrintWriter对象out来向客户端写入字符格式的信息
		out.println("<html>");//向客户端输入信息
		out.println("<head><title>");
		out.println("Servlet演示程序");
		out.println("</title></head>");
		out.println("<body>");
		out.println("<p align=center>这是一个Servlet测试程序");
		out.println("<p align=center><h1>Hello,Java</h1>");
		out.println("</body>");
		out.println("</html>");
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
		//对客户端的"Get"请求进行处理
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
		//对客户端的"Post"请求进行处理
	}
	
	public void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
		//对客户端的"Put"请求进行处理
	}
	
	public void doDelete(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
		//对客户端的"Delete"请求进行处理
	}
	
	public void doOptions(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
		//对客户端的"Options"请求进行处理
	}
	
	public void doTrace(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
		//对客户端的"Trace"请求进行处理
	}
	
	public void destroy(){
		//在Servlet卸载之前进行一些处理工作
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -