一、如何在Tomcat中配置虚拟主机(host)
1.创建页面
创建 index.html 页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> ITBZ </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY> Welcome to ITBZ </BODY>
</HTML>
2.修改server.xml添加Host配置以及添加Context配置
<Host name="www.itbz.com" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="/itbz" docBase="C:\javatest"/>
</Host>
3.修改windows的Host文件
实现域名与IP的绑定 Host文件位置
C:\Windows\System32\drivers\etc
4.访问资源测试结果
二、HttpServletRequest 对象
HttpServletRequest 对象代表客户端浏览器的请求,当客户端浏览器通过 HTTP 协议访问服务器时,HTTP 请求中的所有信息都会被 Tomcat 所解析并封装在这个对象中,通过这个对象提供的方法,可以获得客户端请求的所有信息。
三、获取请求信息
req.getRequestURL() 返回客户端浏览器发出请求时的完整 URL。 req.getRequestURI() 返回请求行中指定资源部分。 req.getRemoteAddr() 返回发出请求的客户机的 IP 地址。 req.getLocalAddr() 返回 WEB 服务器的 IP 地址。 req.getLocalPort() 返回 WEB 服务器处理 Http 协议的连接器所监听
public class DemoServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
StringBuffer requestURL = req.getRequestURL();
String requestURI = req.getRequestURI();
String remoteAddr = req.getRemoteAddr();
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>");
out.println("<HTML>");
out.println("<HEAD><TITLE> ITBZ </TITLE><HEAD>");
out.println("<BODY>");
out.println("URL:"+requestURL+"<br/>");
out.println("URI:"+requestURI+"<br/>");
out.println("RemoteAddr:"+remoteAddr+"<br/>");
out.println("</BODY>");
out.println("</HTML>");
out.flush();
}
}
在web.xml中:
<servlet>
<servlet-name>demoServlet</servlet-name>
<servlet-class>servlet.DemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demoServlet</servlet-name>
<url-pattern>/demo.do</url-pattern>
</servlet-mapping>
结果:
四、获取请求数据
在 Servlet 获取请求数据的方式
req.getParameter(“key”)
根据 key 获取指定 value。
public class GetRequestDataServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>");
out.println("<HTML>");
out.println("<HEAD><TITLE> ITBZ </TITLE><HEAD>");
out.println("<BODY>");
out.println("UserName:"+username+"<br/>");
out.println("</BODY>");
out.println("</HTML>");
out.flush();
}
}
HTML界面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="getInfo.do" method="post">
用户名:<input type="text" name="username"/><br/>
<input type="submit" value="ok"/>
</form>
</body>
</html>
web.xml界面:
<servlet>
<servlet-name>getRequestDataServlet</servlet-name>
<servlet-class>servlet.GetRequestDataServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getRequestDataServlet</servlet-name>
<url-pattern>/getInfo.do</url-pattern>
</servlet-mapping>
五、获取复选框数据
获取复选框(checkbox 组件)中的值
req.getParameterValues(“checkboxkey”)
获取复选框(checkbox 组件)中的值,返回一个 String[]
public class GetRequestDataServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String[] userlikes = req.getParameterValues("userlike");
List<String> strings = Arrays.asList(userlikes);
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>");
out.println("<HTML>");
out.println("<HEAD><TITLE> ITBZ </TITLE><HEAD>");
out.println("<BODY>");
out.println("UserName:"+username+"<br/>");
out.println("UserName:"+strings+"<br/>");
out.println("</BODY>");
out.println("</HTML>");
out.flush();
}
}
HTML页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="getInfo.do" method="post">
用户名:<input type="text" name="username"/><br/>
爱好:<input type="checkbox" name="userlike" value="sport"/>体育
<input type="checkbox" name="userlike" value="music"/>音乐
<input type="submit" value="ok"/>
</form>
</body>
</html>
六、获取所有提交数据的key
获取所有提交数据的 key
req.getParameterNames()
获取请求中所有数据的 key,该方法返回一个枚举
public class GetRequestDataServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String[] userlikes = req.getParameterValues("userlike");
List<String> strings = Arrays.asList(userlikes);
Enumeration<String> parameterNames = req.getParameterNames();
List<String> keys = new ArrayList<>();
while (parameterNames.hasMoreElements()){
keys.add(parameterNames.nextElement());
}
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>");
out.println("<HTML>");
out.println("<HEAD><TITLE> ITBZ </TITLE><HEAD>");
out.println("<BODY>");
out.println("UserName:"+username+"<br/>");
out.println("UserName:"+strings+"<br/>");
out.println("Keys:"+keys+"<br/>");
out.println("</BODY>");
out.println("</HTML>");
out.flush();
}
}
结果:
七、使用 Map 结构获取提交数据
req.getParameterMap()
获取请求中所有的数据并存放到一个 Map 结构中,该方法返回一个 Map,其中 key 为 String 类型 value
public class GetRequestDataServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String[] userlikes = req.getParameterValues("userlike");
List<String> strings = Arrays.asList(userlikes);
Enumeration<String> parameterNames = req.getParameterNames();
List<String> keys = new ArrayList<>();
while (parameterNames.hasMoreElements()){
keys.add(parameterNames.nextElement());
}
Map<String,String[]> parameterMap = req.getParameterMap();
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>");
out.println("<HTML>");
out.println("<HEAD><TITLE> ITBZ </TITLE><HEAD>");
out.println("<BODY>");
out.println("UserName:"+username+"<br/>");
out.println("UserName:"+strings+"<br/>");
out.println("Keys:"+keys+"<br/>");
Iterator<Map.Entry<String,String[]>> iterator = parameterMap.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String,String[]> entry = iterator.next();
out.println("Keys:" + entry.getKey() +" Value:"+ Arrays.asList(entry.getValue()) +"<br/>");
}
out.println("</BODY>");
out.println("</HTML>");
out.flush();
}
}
八、设置请求编码
req.setCharacterEncoding(“utf-8”)
请求的数据包基于字节在网络上传输,Tomcat 接收到请求的数据包后会将数据包中的 字节转换为字符。在 Tomcat 中使用的是 ISO-8859-1 的单字节编码完成字节与字符的转换, 所以数据中含有中文就会出现乱码,可以通过 req.setCharacterEncoding(“utf-8”)方法来对提 交的数据根据指定的编码方式重新做编码处理。
public class GetRequestDataServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String username = req.getParameter("username");
String[] userlikes = req.getParameterValues("userlike");
List<String> strings = Arrays.asList(userlikes);
Enumeration<String> parameterNames = req.getParameterNames();
List<String> keys = new ArrayList<>();
while (parameterNames.hasMoreElements()){
keys.add(parameterNames.nextElement());
}
Map<String,String[]> parameterMap = req.getParameterMap();
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>");
out.println("<HTML>");
out.println("<HEAD><TITLE> ITBZ </TITLE><HEAD>");
out.println("<BODY>");
out.println("UserName:"+username+"<br/>");
out.println("UserName:"+strings+"<br/>");
out.println("Keys:"+keys+"<br/>");
Iterator<Map.Entry<String,String[]>> iterator = parameterMap.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String,String[]> entry = iterator.next();
out.println("Keys:" + entry.getKey() +" Value:"+ Arrays.asList(entry.getValue()) +"<br/>");
}
out.println("</BODY>");
out.println("</HTML>");
out.flush();
}
}
九、资源访问方式
1.绝对路径
绝对路径访问资源表示直接以”/”作为项目的 Content Path。该方式适用于以”/”作为项目的 Content Path
<form action="/getInfo.do" method="post">
2.相对路径
相对路径访问资源表示会相对于项目的 Content Path 作为相对路径。该方式适用于为项目指定的具体的 Content Path
<form action="getInfo.do" method="post">
十、获取请求头信息
req.getHeader(“headerKey”)
根据请求头中的 key 获取对应value
String headerValue = req.getHeader(“headerKey”);
.
req.getHeaderNames()
获取请求头中所有的 key,该方法返回枚举类
Enumeration<>
public class GetRequestHeaderServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String header = req.getHeader("Accept-Language");
Enumeration<String> headerNames = req.getHeaderNames();
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>");
out.println("<HTML>");
out.println("<HEAD><TITLE> ITBZ </TITLE><HEAD>");
out.println("<BODY>");
while (headerNames.hasMoreElements()){
String key = headerNames.nextElement();
String value = req.getHeader(key);
out.println("key:"+key+" Value:"+value+"<br/>");
}
out.println("</BODY>");
out.println("</HTML>");
}
}
web.xml:
<servlet>
<servlet-name>getRequestHeaderServlet</servlet-name>
<servlet-class>servlet.GetRequestHeaderServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getRequestHeaderServlet</servlet-name>
<url-pattern>/getHeader.do</url-pattern>
</servlet-mapping>
结果:
十一、获取请求头案例
需求:编写一个 Servlet,如果浏览器的语言是 zh-CN,显示“你好,聪明的中国人!”,如果浏览器的语言设置为 en-US,那么则显示“Hello,American”
public class LanguageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String header = req.getHeader("accept-language");
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>");
out.println("<HTML>");
out.println("<HEAD><TITLE> ITBZ </TITLE><HEAD>");
out.println("<BODY>");
if (header.indexOf("zh-CN") != -1){
out.println("你好,聪明的中国人!");
}else if(header.indexOf("en-US") != -1){
out.println("hello,American");
}else{
out.println("对不起,不支持该语言!");
}
out.println("</BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
web.xml:
<servlet>
<servlet-name>languageServlet</servlet-name>
<servlet-class>servlet.LanguageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>languageServlet</servlet-name>
<url-pattern>/language.do</url-pattern>
</servlet-mapping>
|