ServletDemo14.java
package com.taotao.web;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@SuppressWarnings({"all"})
public class ServletDemo14 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//使用reguest对象 获取请求数据
String name = req.getParameter("name"); //url?name=zhangsan
//使用response对象 设置响应数据
resp.setHeader("content-type","text/html;charset=utf-8");
resp.getWriter().write(""
+ name + ",欢迎你!");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("post...");
}
}
package com.taotao.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@SuppressWarnings({"all"})
@WebServlet("/req1")
public class RequestDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//- String getMethod():获取请求方式
String method = req.getMethod();
System.out.println(method); //GET
//- String getContextPath():获取虚拟目录(项目访问路径)
String contextPath = req.getContextPath();
System.out.println(contextPath); // tomcat_demo02
//- StringBuffer getRequesURL():获取URL(统一资源定位符)
StringBuffer requestURL = req.getRequestURL();
System.out.println(requestURL.toString()); //http://localhost:8080/tomcat_demo02/req1
//- String getRequestURI():获取URL(统一资源标识符)
String requestURI = req.getRequestURI();
System.out.println(requestURI); ///tomcat_demo02/req1
//- String getQueryString():获取请求参数(GET方式)
String queryString = req.getQueryString();
System.out.println(queryString); //问号后面的参数
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
从用户输入获取请求参数,返回给服务器端
package com.taotao.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
@SuppressWarnings({"all"})
@WebServlet("/req2")
public class RequestDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get...");
//1.获取所有参数的Map集合
Map<String, String[]> map = req.getParameterMap();
for (String key:map.keySet()){
System.out.println(key + ":");
//获取值
String[] values = map.get(key);
for (String value: values) {
System.out.print(value + " ");
}
System.out.println();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//我们调用doGet方法,使用相同的逻辑
this.doGet(req, resp);
}
}
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<form action="/tomcat_demo02/req2" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit">
form>
body>
html>
package com.taotao.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
@SuppressWarnings({"all"})
@WebServlet("/req2")
public class RequestDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get...");
//1.获取所有参数的Map集合
Map<String, String[]> map = req.getParameterMap();
for (String key:map.keySet()){
System.out.println(key + ":");
//获取值
String[] values = map.get(key);
for (String value: values) {
System.out.print(value + " ");
}
System.out.println();
}
//2.根据key获取参数值,数组
String[] hobbies = req.getParameterValues("hobby");
for (String hobby: hobbies){
System.out.println(hobby);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//我们调用doGet方法,使用相同的逻辑
this.doGet(req, resp);
}
}
System.out.println("------------------");
//3.根据key 获取单个参数值
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println(username);
System.out.println(password);
在doPost()方法中调用doGet()
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//我们调用doGet方法,使用相同的逻辑
this.doGet(req, resp);
}
get与post结果相同,不再演示