Servlet程序演示doGet()和doPost()方法

为什么80%的码农都做不了架构师?>>>   hot3.png

15103807_M5iv.gif 15103807_iQKe.gif View Code
package com.wyf.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.Servlet;
import javax.servlet.ServletException;

public class FirstServlet extends HttpServlet implements Servlet {
public FirstServlet() {
super();
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.log("执行doGet()方法... "); // 调用Servlet自带的日志输出信息到控制台
this.execute(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.log("执行doPost()方法... "); // 调用Servlet自带的日志输出信息到控制台
this.execute(req, resp);
}

public void execute(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setCharacterEncoding("UTF-8"); // 设置resp的编码格式
req.setCharacterEncoding("UTF-8"); // 设置resp的编码格式
String reqURL = req.getRequestURI();// 设置访问该Servlet的URL
String method = req.getMethod(); // 设置访问Servlet的方法,GET活POST
String param = req.getParameter("param");// 设置客户端提交的参数param的值

resp.setContentType("text/html"); // 设置文档类型为HTML
PrintWriter out = resp.getWriter();

out
.println("");
out.println("");
out.println("A Servlet");
out.println("");
out.println("以" + method + "方式访问该页面,取到的param参数为:" + param + "
");
out
.println("
");
out
.println("
");

// 有客户端浏览器读取该文档的更新时间
out
.println("");
out.println("");
out.println("");
out.flush();
out.close();
}
}

运行结果:

(1) FirstServlet: 执行doPost()方法...

Servlet程序演示doGet()和doPost()方法_第1张图片

(2)FirstServlet: 执行doGet()方法...

Servlet程序演示doGet()和doPost()方法_第2张图片

这里显示了doGet()和doPost()方法的区别,区别总结如下,

Servlet程序演示doGet()和doPost()方法_第3张图片

 

 


 

转载于:https://my.oschina.net/garyun/blog/602919

你可能感兴趣的:(Servlet程序演示doGet()和doPost()方法)