1.URL的匹配规则
@WebServlet("/test")
/test就是咱们某一个资源的路径写法
用户申请了一个资源
http://localhost:8080/day41_wb/LoginServlet
1.http协议
2.localhost 本地服务器主机的名字
3.8080端口号
4.day41_wb/LoginServlet是咱们所申请的资源
day41_wb/当前应用程序目录,就是程序的绝对路径
LoginServlet 申请的资源
url匹配原则:
1.精准匹配
比如 /资源名字@WebServlet("/test.do")
2模糊匹配符
比如 @WebServlet("*.do") 以.do为后缀的资源都可以进行访问
比如@WebServlet("/*") 所有资源都可以访问
package com.javacoffee.servlet;
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("*.do")
public class TestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.getWriter().append("这是模糊匹配,后缀名为do的");
}
}
?
package com.javacoffee.servlet;
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("/TestServlet2")
public class TestServlet2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.getWriter().append("这个是精准查询");
}
}
?
?
package com.javacoffee.servlet;
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("/*")
public class TestServlet1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.getWriter().append("这个是模糊查询所有的资源");
}
}
?
2.ServletContext【重要】
ServletContext接口,实例化以后是一个对象,
是咱们当前应用程序可以使用的唯一对象。
在一个Servlet里面,需要使用另外一个Servlet的提供的数据,怎么办?咱们会想到转发,但是如果不用转发怎么办?这时候就需要使用ServletContext对象,是全局的。比如你在一个Servlet里面设置一个数据,想要在另外一个Servlet里面使用这个数据。可以通过ServletContext这个接口,这个对象是全局的。
如果你在一个Servlet里面使用servletContext 对象设置一个数据,在另外一个Servlet里面也可以通过ServletContext这个对象获取这个数据
两个Servlet依靠servletContext对象进行通信,分享一些数据
例如TestServlet2需要获取TestServlet1的资源
package com.javacoffee.servlet;
import com.javacoffee.enity.Person;
import javax.servlet.ServletContext;
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("/TestServlet1")
public class TestServlet1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
//1.获取servletContext对象没当前上下文对象
ServletContext servletContext = request.getServletContext();
//2.给当前上下文对象设置一部分数据
servletContext.setAttribute("name","张三");
//也可以传入对象
servletContext.setAttribute("person",new Person(21,"李四","特别喜欢吃河南烩面"));
String serverInfo = servletContext.getServerInfo();//返回的是当前Servlet的详细信息
String contextPath = servletContext.getContextPath();//项目的根目录(以后出现404可以先找这个根目录然后再一点一点的找)
System.out.println(serverInfo);
System.out.println(contextPath);
}
}
package com.javacoffee.servlet;
import javax.servlet.ServletContext;
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("/TestServlet2")
public class TestServlet2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
ServletContext servletContext = request.getServletContext();
Object name = servletContext.getAttribute("name");
Object person = servletContext.getAttribute("person");
response.getWriter().println(name+";;;"+person);
}
}
?
?
3.Request对象
ServletReuqest接口
用户访问服务器,服务器会生成一个对象包含了http所有请求头
由于使用的是http协议,HttpServletRequest
3.1RequestAPI
getRequestURL(); 获取完成的url
getRequestURI();获取资源名字
getQueryString();获取一个url参数部分
getRemoteAddr();返回的是客户端的ip地址
getRemoteUser();返回的是客户端的用户
getRemotePort();返回的是客户端的主机的端口号
getRemoteHost();返回的是客户端的主机地址
getCookie();获取CookieD对象
getSession();获取Session对象
getLocalName();获取Web服务器主机的名字
getServletContext();获取上下文对象的
setCharacterEncoding();设置编码集的
getParameter();获取前端传过来的数据的
setAttribute();将数据设置给request对象
getAttribute();获取request对象中的数据的
package com.qfedu.c_request;
?
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("/request2")
public class RequestServlet2 extends HttpServlet {
? ?protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
? ? ? ?doGet(request, response);
? }
?
? ?protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
? ? ? ?String remoteAddr = request.getRemoteAddr();
? ? ? ?String remoteHost = request.getRemoteHost();
? ? ? ?String remoteUser = request.getRemoteUser();
? ? ? ?int remotePort = request.getRemotePort();
? ? ? ?System.out.println(remoteAddr);//ip地址
? ? ? ?System.out.println(remoteHost);//主机的地址
? ? ? ?System.out.println(remoteUser);//用户名字
? ? ? ?System.out.println(remotePort);//端口号
? ? ? ?//以上代码如果你们在同一个网段是可以测试的
?
? ? ? ?//发现打印的都是别人访问的东西。意味着从这地方知道,
? ? ?
? ? ? ?//以后开发必用的!!!是以下的方法
? ? ? String name = request.getParameter("name");//获取前段传过来的数据
? ? ? request.setCharacterEncoding("utf-8");//请求设置编码集
? ? ? request.getRequestDispatcher("index.jsp").forward(request, response);//转发的代码
? ? ? request.setAttribute("name", "狗蛋");
? ? ? request.getAttribute("name");
? ? ? ?//request.getServletContext();//获取上文对象的
? }
}
?
4.Response对象
响应对象,把数据给客户端
咱们的Servlet紧紧围绕着两个点学习(Request, Response)请求和响应
setHeader(); 设置响应头的参数
setContentType();设置字符编码集
getWriter();获取字符输出流对象
addCoookie(); 对浏览器新增一个Cookie
sendRedirect();重定向
这个在我之前的博客也都用到过,这里也不细讲了
|