什么是Servlet
一个简单的Servlet
public class helloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.println("hello servlet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.govd.servlet.helloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/s1</url-pattern>
</servlet-mapping>
servlet原理
映射Mapping
servletContext
public class helloservlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// this.getInitParameter(); //获取初始化参数
// this.getServletConfig(); //获取servlet配置
// this.getServletContext() //获取servlet上下文
ServletContext servletContext = this.getServletContext();
servletContext.setAttribute("username","govd");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
public class getServlet extends helloServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String username =(String) servletContext.getAttribute("username");
resp.setHeader("Content-Type","text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().println("你好"+username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
ServletContext应用
获取初始化参数
请求转发
请求转发:请求转发相当于请外包,将请求发给/s3完成,但还是在本url页面呈现
重定位:(我做不了,你找别人吧),转到别的url(页面)
public class servletDispatch extends helloservlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
//RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/s3"); 请求转发的路径
//requestDispatcher.forward(req,resp); 调用forward实现请求转发
servletContext.getRequestDispatcher("/s3").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
读取资源文件
配置文件
driver=com.jdbc.mysql.driver
url=jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=123456
获取配置信息
public class getproperties extends helloservlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties properties = new Properties();
properties.load(in);
String username = properties.getProperty("username");
String password = properties.getProperty("password");
resp.getWriter().println(username+":"+password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}