tomcat初使用
package com.jxb;
import javax.servlet.*;
import java.io.IOException;
public class servlet implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("bb");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>helloservlet</servlet-name>
<servlet-class>com.jxb.servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloservlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
代码总章
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--老韩小技巧: web.xml主要用来配置该web应用使用到的Servlet
老师解读
1. servlet-name: 给Servlet取名(程序员决定), 该名字唯一
2. servlet-class: Servlet的类的全路径: Tomcat在反射生成该Servlet需要使用
3. url-pattern: 这个就是该servlet访问的url的配置(路径)
4. 这时我们应该这样访问servlet http://localhost:8080/servlet/helloServlet
5. url-pattern 取名是程序员决定的
6. load-on-startup 表示在tomcat 启动时,会自动的加载servlet实例
小技巧: alt+r => 老师配置
-->
<!-- 配置HelloServlet -->
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.hspedu.servlet.HelloServlet</servlet-class>
<!--<load-on-startup>1</load-on-startup>-->
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/helloServlet</url-pattern>
</servlet-mapping>
<!-- <!– 配置HiServlet –>-->
<!-- <servlet>-->
<!-- <servlet-name>HiServlet</servlet-name>-->
<!-- <servlet-class>com.hspedu.servlet.HiServlet</servlet-class>-->
<!-- </servlet>-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>HiServlet</servlet-name>-->
<!-- <url-pattern>/hiServlet</url-pattern>-->
<!-- </servlet-mapping>-->
<!-- <!–这里是idea自动生成的配置–>-->
<!-- <servlet>-->
<!-- <servlet-name>OkServlet</servlet-name>-->
<!-- <servlet-class>com.hspedu.servlet.OkServlet</servlet-class>-->
<!-- </servlet>-->
<!-- <!–对OkServlet进行url配置–>-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>OkServlet</servlet-name>-->
<!-- <url-pattern>/okServlet</url-pattern>-->
<!-- </servlet-mapping>-->
<!-- <!–配置CatServlet–>-->
<!-- <servlet>-->
<!-- <servlet-name>CatServlet</servlet-name>-->
<!-- <servlet-class>com.hspedu.servlet.homework.CatServlet</servlet-class>-->
<!-- </servlet>-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>CatServlet</servlet-name>-->
<!-- <url-pattern>/cat</url-pattern>-->
<!-- </servlet-mapping>-->
<!-- <!–配置DogServlet–>-->
<!-- <servlet>-->
<!-- <servlet-name>DogServlet</servlet-name>-->
<!-- <servlet-class>com.hspedu.servlet.homework.DogServlet</servlet-class>-->
<!-- </servlet>-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>DogServlet</servlet-name>-->
<!-- <url-pattern>/dog</url-pattern>-->
<!-- </servlet-mapping>-->
<!-- <!–配置DBServlet–>-->
<!-- <servlet>-->
<!-- <servlet-name>DBServlet</servlet-name>-->
<!-- <servlet-class>com.hspedu.servlet.DBServlet</servlet-class>-->
<!-- <!–配置信息,而不是硬编码到程序–>-->
<!-- <init-param>-->
<!-- <param-name>username</param-name>-->
<!-- <param-value>hsp</param-value>-->
<!-- </init-param>-->
<!-- <init-param>-->
<!-- <param-name>pwd</param-name>-->
<!-- <param-value>123456</param-value>-->
<!-- </init-param>-->
<!-- </servlet>-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>DBServlet</servlet-name>-->
<!-- <url-pattern>/db</url-pattern>-->
<!-- </servlet-mapping>-->
<!-- <!–配置ServletContext_–>-->
<!-- <servlet>-->
<!-- <servlet-name>ServletContext_</servlet-name>-->
<!-- <servlet-class>com.hspedu.servlet.servletcontext.ServletContext_</servlet-class>-->
<!-- </servlet>-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>ServletContext_</servlet-name>-->
<!-- <url-pattern>/servletContext_</url-pattern>-->
<!-- </servlet-mapping>-->
<!-- <servlet>-->
<!-- <servlet-name>OrderServlet</servlet-name>-->
<!-- <servlet-class>com.hspedu.servlet.servletcontext.OrderServlet</servlet-class>-->
<!-- </servlet>-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>OrderServlet</servlet-name>-->
<!-- <url-pattern>/orderServlet</url-pattern>-->
<!-- </servlet-mapping>-->
<!-- <servlet>-->
<!-- <servlet-name>PayServlet</servlet-name>-->
<!-- <servlet-class>com.hspedu.servlet.servletcontext.PayServlet</servlet-class>-->
<!-- </servlet>-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>PayServlet</servlet-name>-->
<!-- <url-pattern>/payServlet</url-pattern>-->
<!-- </servlet-mapping>-->
<!-- <servlet>-->
<!-- <servlet-name>HttpServletRequestMethods</servlet-name>-->
<!-- <servlet-class>com.hspedu.servlet.request.HttpServletRequestMethods</servlet-class>-->
<!-- </servlet>-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>HttpServletRequestMethods</servlet-name>-->
<!-- <url-pattern>/requestMethods</url-pattern>-->
<!-- </servlet-mapping>-->
<!-- <servlet>-->
<!-- <servlet-name>CheckServlet</servlet-name>-->
<!-- <servlet-class>com.hspedu.servlet.request.CheckServlet</servlet-class>-->
<!-- </servlet>-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>CheckServlet</servlet-name>-->
<!-- <url-pattern>/checkServlet</url-pattern>-->
<!-- </servlet-mapping>-->
<!-- <servlet>-->
<!-- <servlet-name>ManageServlet</servlet-name>-->
<!-- <servlet-class>com.hspedu.servlet.request.ManageServlet</servlet-class>-->
<!-- </servlet>-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>ManageServlet</servlet-name>-->
<!-- <url-pattern>/manageServlet</url-pattern>-->
<!-- </servlet-mapping>-->
<!-- <servlet>-->
<!-- <servlet-name>RegisterServlet</servlet-name>-->
<!-- <servlet-class>com.hspedu.servlet.request.homework.RegisterServlet</servlet-class>-->
<!-- </servlet>-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>RegisterServlet</servlet-name>-->
<!-- <url-pattern>/registerServlet</url-pattern>-->
<!-- </servlet-mapping>-->
<!-- <servlet>-->
<!-- <servlet-name>ComputerServlet</servlet-name>-->
<!-- <servlet-class>com.hspedu.servlet.request.homework.ComputerServlet</servlet-class>-->
<!-- </servlet>-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>ComputerServlet</servlet-name>-->
<!-- <url-pattern>/computerServlet</url-pattern>-->
<!-- </servlet-mapping>-->
<!-- <servlet>-->
<!-- <servlet-name>DownServlet</servlet-name>-->
<!-- <servlet-class>com.hspedu.servlet.response.DownServlet</servlet-class>-->
<!-- </servlet>-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>DownServlet</servlet-name>-->
<!-- <url-pattern>/downServlet</url-pattern>-->
<!-- </servlet-mapping>-->
<!-- <servlet>-->
<!-- <servlet-name>DownServletNew</servlet-name>-->
<!-- <servlet-class>com.hspedu.servlet.response.DownServletNew</servlet-class>-->
<!-- </servlet>-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>DownServletNew</servlet-name>-->
<!-- <url-pattern>/downServletNew</url-pattern>-->
<!-- </servlet-mapping>-->
<!-- <servlet>-->
<!-- <servlet-name>MyPayServlet</servlet-name>-->
<!-- <servlet-class>com.hspedu.servlet.response.homework.MyPayServlet</servlet-class>-->
<!-- </servlet>-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>MyPayServlet</servlet-name>-->
<!-- <url-pattern>/myPayServlet</url-pattern>-->
<!-- </servlet-mapping>-->
<!-- <!–配置整个网站的信息–>-->
<!-- <context-param>-->
<!-- <param-name>website</param-name>-->
<!-- <param-value>http://www.hanshunping.net</param-value>-->
<!-- </context-param>-->
<!-- <context-param>-->
<!-- <param-name>company</param-name>-->
<!-- <param-value>韩顺平教育</param-value>-->
<!-- </context-param>-->
</web-app>
package com.hspedu.servlet;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class HelloServlet implements Servlet {
private int count = 0;
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("init() 被调用");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest,
ServletResponse servletResponse) throws ServletException, IOException {
count++;
System.out.println("hi HelloServlet~ count= " + count);
System.out.println("当前线程id= " + Thread.currentThread().getId());
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
String method = httpServletRequest.getMethod();
if("GET".equals(method)) {
doGet();
} else if("POST".equals(method)) {
doPost();
}
}
public void doGet() {
System.out.println("doGet() 被调用..");
}
public void doPost() {
System.out.println("doPost() 被调用..");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
System.out.println("destroy() 被调用...");
}
}
hi HelloServlet~ count= 2
当前线程id= 26
doGet() 被调用..
tomcat入门
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class jxb {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9998);
while (!serverSocket.isClosed()) {
System.out.println("等待监听");
Socket socket = serverSocket.accept();
OutputStream outputStream = socket.getOutputStream();
BufferedReader bufferedReader = new BufferedReader(new
FileReader("src/hello.html"));
String buf="";
while ((buf=bufferedReader.readLine())!=null){
outputStream.write(buf.getBytes());
}
outputStream.write("hello,my dear jack".getBytes());
outputStream.close();
socket.close();
}
serverSocket.close();
}
}
作业
package com.hspedu.servlet.homework;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class CatServlet implements Servlet {
private int count = 0;
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
System.out.println("访问的方式= " + httpServletRequest.getMethod());
System.out.println("访问CatServlet的次数= " + (++count));
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
作业2
package com.hspedu.servlet.homework;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class DogServlet extends HttpServlet {
private int getCount = 0;
private int postCount = 0;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("DogServlet doPost 访问次数= " + (++postCount));
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("DogServlet doGet 访问次数= " + (++getCount));
}
}
作业3
package com.hspedu.servlet.homework;
import javax.servlet.ServletConfig;
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;
@WebServlet(urlPatterns = {"/pig1", "/pig2"}, loadOnStartup = 1)
public class PigServlet extends HttpServlet {
private int getCount = 0;
private int postCount = 0;
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println("PigServlet init() 被调用~");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("访问的浏览器的ip= " + req.getRemoteAddr());
System.out.println("PigServlet 的 doPost方法被访问了 " + (++postCount));
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("访问的浏览器的ip= " + req.getRemoteAddr());
System.out.println("PigServlet 的 doGet方法被访问了 " + (++getCount));
}
}
配置方式注解
package com.annotation;
import javax.jws.WebService;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns={"/ok1","/ok2"},initParams = @WebInitParam(name = "xx",
value = "yy"))
public class serevlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("注解doget");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("注解dopost");
}
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println("注解init被调用");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!--load表示在tomcat启动时自动去加载servlet-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- <servlet>-->
<!-- <servlet-name>helloservlet</servlet-name>-->
<!-- <servlet-class>com.jxb.servlet</servlet-class>-->
<!-- <load-on-startup>1</load-on-startup>-->
<!-- </servlet>-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>helloservlet</servlet-name>-->
<!-- <url-pattern>/hello</url-pattern>-->
<!-- </servlet-mapping>-->
<!-- 配置hservlet-->
<servlet>
<servlet-name>hservlet</servlet-name>
<servlet-class>com.hservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hservlet</servlet-name>
<url-pattern>/hservlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>okservlet</servlet-name>
<servlet-class>com.okServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>okservlet</servlet-name>
<url-pattern>/okservlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>catservlet</servlet-name>
<servlet-class>com.jxb.homework.catservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>catservlet</servlet-name>
<url-pattern>/cat</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dogservlet</servlet-name>
<servlet-class>com.jxb.homework.dogservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dogservlet</servlet-name>
<url-pattern>/dog</url-pattern>
</servlet-mapping>
</web-app>
继承类配置
package com;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class hservlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doget");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("dopost");
}
}
重定向
package com.hspedu.http.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class t1Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect("http://www.baidu.com");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
浏览器登陆
package com.hspedu.http.servlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws javax.servlet.ServletException,
IOException {
System.out.println("jack");
response.setContentType("text/html;charset=utf-8");
PrintWriter writer= response.getWriter();
writer.print("登陆成功");
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
System.out.println("我是帅哥");
response.setContentType("text/html;charset=utf-8");
PrintWriter writer= response.getWriter();
writer.print("登陆成功");
writer.flush();
writer.close();
}
}
浏览器访问次数
package com.hspedu.servlet.servletcontext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class OrderServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = getServletContext();
Integer visit_count = WebUtils.visitCount(servletContext);
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.print("新网站被访问的次数是"
+ visit_count + "");
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
package com.hspedu.servlet.servletcontext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class PayServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = getServletContext();
Integer visit_count = WebUtils.visitCount(servletContext);
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.print("新网站被访问的次数是"
+ visit_count + "");
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
package com.hspedu.servlet.servletcontext;
import javax.servlet.ServletContext;
public class WebUtils {
public static Integer visitCount(ServletContext servletContext) {
Object visit_count = servletContext.getAttribute("visit_count");
if (visit_count == null) {
servletContext.setAttribute("visit_count", 1);
visit_count = 1;
} else {
visit_count = Integer.parseInt(visit_count + "") + 1;
servletContext.setAttribute("visit_count", visit_count);
}
return Integer.parseInt(visit_count + "");
}
}
servletcontext使用方法
package com.hspedu.servlet.servletcontext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ServletContext_ extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = getServletContext();
String website = servletContext.getInitParameter("website");
String company = servletContext.getInitParameter("company");
String contextPath = servletContext.getContextPath();
String realPath = servletContext.getRealPath("/");
System.out.println("项目路径= " + contextPath);
System.out.println("website= " + website);
System.out.println("company= " + company);
System.out.println("项目发布后的绝对路径= " + realPath);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
request入门方法
package com.hspedu.servlet.request;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class HttpServletRequestMethods extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("HttpServletRequestMethods doPost() 被调用..");
System.out.println("请求的资源路径URI= " + request.getRequestURI());
System.out.println("请求的统一资源定位符(绝对路径)URL= " + request.getRequestURL());
System.out.println("请求的客户端ip 地址= " + request.getRemoteAddr());
System.out.println("http请求头HOST= " + request.getHeader("Host"));
System.out.println("该请求的发起地址是= " + request.getHeader("Referer"));
String userAgent = request.getHeader("User-Agent");
System.out.println("User-Agent= " + userAgent);
String[] s = userAgent.split(" ");
System.out.println("浏览器=" + s[s.length - 1].split("\\/")[0]);
String cookie = request.getHeader("Cookie");
String JSESSIONID = cookie.split("=")[1];
System.out.println("取出JSESSIONID= " + JSESSIONID);
System.out.println("http请求方式~= " + request.getMethod());
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
String[] hobbies = request.getParameterValues("hobby");
System.out.println("username= " + username);
System.out.println("pwd= " + pwd);
for (String hobby : hobbies) {
System.out.println("hobby=" + hobby);
}
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.print("提交的用户名= "
+ username + "");
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
HttpServletRequestMethods doPost() 被调用..
请求的资源路径URI= /servlet/methods
请求的统一资源定位符(绝对路径)URL= http://localhost:8080/servlet/methods
请求的客户端ip 地址= 127.0.0.1
http请求头HOST= localhost:8080
该请求的发起地址是= http://localhost:8080/servlet/request.html
User-Agent= Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 SE 2.X MetaSr 1.0
浏览器=1.0
取出JSESSIONID= A130727EE668306F7E430FF23ECAF8A1; Idea-8296e770
http请求方式~= POST
username= 见下表
pwd= 456
hobby=hsp
转发请求使用
package com.hspedu.servlet.request;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CheckServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("CheckServlet 被调用..");
String username = request.getParameter("username");
if ("tom".equals(username)) {
request.setAttribute("role", "管理员");
} else {
request.setAttribute("role", "普通用户");
}
RequestDispatcher requestDispatcher =
request.getRequestDispatcher("/manage");
requestDispatcher.forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
package com.hspedu.servlet.request;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class ManageServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("ManageServlet 被调用..");
String username = request.getParameter("username");
String role = (String) request.getAttribute("role");
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.print("用户名: " + username + "
");
writer.print("角色 : " + role);
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
重定向的使用
package com.hspedu.servlet.response;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class DownServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String contextPath = getServletContext().getContextPath();
System.out.println("contextPath= " + contextPath);
response.sendRedirect(contextPath + "/downnew");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
package com.hspedu.servlet.response;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class DownServletNew extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("DownServletNew 被调用");
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.print("ok");
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
作业完成
package com.hspedu.servlet.response.homework;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class MyPayServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String money = request.getParameter("money");
System.out.println("money= " + money.getClass());
int iMoney = WebUtils.parseString(money);
ServletContext servletContext = getServletContext();
if(iMoney > 100) {
response.sendRedirect(servletContext.getContextPath() + "/payok.html");
} else {
response.sendRedirect(servletContext.getContextPath() + "/pay.html");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
package com.hspedu.servlet.response.homework;
public class WebUtils {
public static int parseString(String str) {
int num = 0;
try {
num = Integer.parseInt(str);
} catch (NumberFormatException e) {
System.out.println("输入的str格式不正确...");
}
return num;
}
}
web路径问题
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>b.html</title>
<!--
老韩解读
1. 如果没有<base href="http://localhost:10000/hspedu_servlet/">
2. 当点击 返回a.html 超链接,将会以当前浏览器的地址为路径来确定 路径
3. 如果增加了<base href="http://localhost:10000/hspedu_servlet/">
4. 将以 base 指定的href的地址为路径,来确定 超链接的路径
-->
<!--<base href="http://localhost:10000/hspedu_servlet/">-->
<!--简写形式-->
<!--使用base 标签搞定
老韩解读
1. 下面老师写的 base href="http://localhost:8080/webpath/"
2. 表示的含义就是当前这个页面的访问所有资源都是以 http://localhost:8080/webpath/ 参照
-->
<!--<base href="http://localhost:8080/webpath/">-->
<!--上面的写法可以简化一把-->
<!--
老韩解读
1. base 标签是哪个在解析? => 浏览器
2. 浏览器在解析 第一个 /时候,会解析成 http://localhost:8080/
3. 浏览器 href="/webpath/" =解析=> href="http://localhost:8080/webpath/"
4. 浏览器 <a href="a.html">返回a.html~</a> , 参考 base => 最后
href="http://localhost:8080/webpath/a.html"
-->
<base href="/webpath/">
</head>
<body>
<h1>这是/d1/d2/b.html</h1>
<!--
老韩解读
1. 返回a.html => 使用前面的相对路径http://localhost:8080/webpath/d1/d2/
2. ../../a.html => http://localhost:8080/webpath/a.html
3. 相对路径会让这个项目相互调用的关系变得复杂
4. 使用base 标签搞定
-->
<!--<a href="../../a.html">返回a.html~</a>-->
<a href="a.html">返回a.html~</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户页面</title>
<!--
1. 使用相对路径: <a href="../../login.html">点击返回登录</a>
2. 使用相对路径是比较麻烦,即使成功,还需要考虑是直接定位到这个资源还是请求转发到这个资源
3. 使用base 标签给一个固定的相对路径
4. <base href="/webpath/"> 该标签是浏览器解析 http://localhost:8080/webpath/
5. 在 base 得到的url 路径基础上 <a href="login.html">点击返回登录</a> =>
http://localhost:8080/webpath/login.html
6. 一定要自己 玩一把,再听评讲,才会有融合贯通.
-->
<base href="/webpath/">
</head>
<body>
<h1>用户页面</h1>
<a href="login.html">点击返回登录</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>a.html</title>
</head>
<body>
<h1>这是a.html~~~~~</h1>
<!-- 相对路径
1. href="d1/d2/b.html" 等价于 http://localhost:8080/项目名/d1/d2/b.html
-->
<a href="d1/d2/b.html">跳转到/d1/d2/b.html</a>
<br/><br/>
<!--
老韩解析
1. 在实际开发中,往往不是直接访问一个资源的而是在服务端进行转发或者重定向来访问资源
2. 演示转发定位 b.html
3. href="servlet03" http://localhost:8080/webpath/servlet03
-->
<a href="servlet03">转发到/d1/d2/b.html</a>
</body>
</html>
路径问题代码2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>base 标签</title>
</head>
<body>
<h1>注册用户~~</h1>
<!--
老韩解读:
1. 可以看到,我们原来的访问一个web资源的路径非常的麻烦
2. 目前我们访问的是 ok的资源
3. 我们可以使用相对路径来解决
4. http://localhost:8080/webpath/ok => ok
5. 如果你action="ok" 等价 http://localhost:8080/webpath/ok
-->
<form action="ok"
method="post">
u: <input type="text" name="username"/><br><br>
<input type="submit" value="注册用户"/>
</form>
<h1>讨论区~</h1>
<form action="ok"
method="post">
讨论内容: <textarea cols="50" rows="5"></textarea><br><br>
<input type="submit" value="发布讨论"/>
</form>
<h1>回复区~</h1>
<form action="ok"
method="post">
回复内容: <textarea cols="50" rows="5"></textarea><br><br>
<input type="submit" value="回复"/>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h1>用户登录</h1>
<!--
请写出 login.html 在不通过Servlet转发情况下, 如何 通过表单提交, 找到 user.html, 把所有的写法列出来
1. 完整的url http://localhost:8080/webpath/views/user/user.html
2. 使用相对路径 action="views/user/user.html" => 使用当前浏览器的 地址栏 ip:port/工程路径/ + ...
3. action="/webpath/views/user/user.html"
浏览器对第一个 / 进行解析 为 http://localhost:8080/ + webpath/views/user/user.html
同样是一个完整url
4. 这里老师提一句: 我们使用服务器渲染技术,可以动态得到 /工程路径 , 后面再说
-->
<!--<form action="/webpath/hi" method="post">-->
<form action="/webpath/redirect" method="post">
u: <input type="text" name="username"><br/>
<input type="submit" value="登录">
</form>
</body>
</html>
路径转发以及重定向
package com.jxb.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HelloServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("HelloServlet 被调用...");
String realPath = getServletContext().getRealPath("/");
System.out.println("realPath= " + realPath);
String contextPath = getServletContext().getContextPath();
System.out.println("contextPath= " + contextPath);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
package com.jxb.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HiServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/views/user/user.html")
.forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
代码继续
package com.jxb.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class RedirectServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("RedirectServlet 重定向4 被调用....");
String contextPath = getServletContext().getContextPath();
response.sendRedirect(contextPath + "/views/user/user.html");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
package com.jxb.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Servlet03 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Servlet03 进行转发~~~ ....");
request.getRequestDispatcher("/d1/d2/b.html").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}