Get : 链接包含报文内容
GET 方法向页面请求发送已编码的用户信息。页面和已编码的信息中间用 ? 字符分隔,如下所示:
http://127.0.0.1:8080/Servlets/ReidDemo1?name=Reid&url=http://127.0.0.1:8080/Servlets/ReidDemo1
代码示例:
package com.reid.Demo1;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class RiedDemo Get()
*/
public class ReidDemo extends HttpServlet {
public static final long serialVersionID = 1L;
//继承默认构造方法
public ReidDemo(){
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String title = "使用 GET 方法读取表单数据";
// 处理中文 服务初始页面
if(null == request.getParameter("name"))
{
String docType = "<!DOCTYPE html> \n";
out.println(docType +
"<html>\n" +
"<head><title>" + "菜鸟教程(runoob.com)" + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + "菜鸟教程(runoob.com)" + "</h1>\n" +
"<ul>\n" +
" <form action=\"ReidDemo1\" method=\"GET\">\n" +
"网址名:<input type=\"text\" name=\"name\">\n" +
"<br />\n" +
"网址:<input type=\"text\" name=\"url\" />\n" +
"<input type=\"submit\" value=\"提交\" />\n" +
"</form>"
+ "\n" +
"</ul>\n" +
"</body></html>");
}
else{
String name = new String(request.getParameter("name").getBytes("ISO8859-1"), "UTF-8");
String docType = "<!DOCTYPE html> \n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<ul>\n" +
" <li><b>站点名</b>:"
+ name + "\n" +
" <li><b>网址</b>:"
+ request.getParameter("url") + "\n" +
"</ul>\n" +
"</body></html>");
}
}
}
效果图:
使用Get方法提交:
<form action=\"ReidDemo1\" method=\"GET\">
action 必须和servlet name保持一致:
<servlet-name>ReidDemo1</servlet-name>
提交: