Servlet是sun公司开发动态web的一门技术
sun在这些API中提供一个借口叫做:Servlet,如果你想开发一个Servlet程序,只需要完成两个小步骤:
1、编写一个类,实现Servlet接口
2、把开发好的Java类部署到web服务器中。
把实现了Servlet接口的Java程序叫做,Servlet
1、构建一个普通的Maven项目,删掉里面的src目录,以后我们的学习就在这个项目里面建立Moudel;这个空的工程就是Maven主工程;
2、关于Maven父子工程的理解:
父项目中会有
servlet
子项目会有
javaweb-servlet com.dandelion 1.0-SNAPSHOT
3、Maven环境优化
1,修改web.xml为最新的
2,将maven的结构搭建完整
4、编写一个Servlet程序
1,编写一个普通类
2,实现Servlet接口,这里我们直接继承HttpServlet
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("进入doGet方法");
PrintWriter writer = resp.getWriter();
writer.print("Hello Servlet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
5、编写Servlet的映射
Java程序要通过浏览器访问,而浏览器需要连接web服务器,所以我们需要在web服务器中注册我们写的Servlet,还需要给他一个浏览器能够访问的路径;
hello com.dandelion.servlet.HelloServlet hello /hello
6、配置Tomcat
配置发布的路径就可以了
7、启动测试
1、一个Servlet可以指定一个映射路径
hello /hello
2、一个Servlet可以指定多个映射路径
hello /hello hello /hello1
3、一个Servlet可以指定通用映射路径
hello /hello/*
4、默认请求路径
hello /*
5、指定一些后缀或者前缀等等
hello *.dandelion
6、优先级问题
指定了固有映射路径优先级最高,如果找不到就会默认的处理请求
public class ErrorServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
PrintWriter writer = resp.getWriter();
writer.print("404");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
1.4、ServletContexterror com.dandelion.servlet.ErrorServlet error /*
1、共享数据
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String username = "dandelion";
context.setAttribute("username",username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String username = (String) context.getAttribute("username");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().print("名字"+username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
getc com.dandelion.servlet.GetServlet getc /getc
2、获取初始化参数
public class InitpServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
resp.getWriter().print(url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
url jdbc:mysql://localhost:3306/mybatis initp com.dandelion.servlet.InitpServlet initp /initp
3、请求转发
public class RfServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
// RequestDispatcher requestDispatcher = context.getRequestDispatcher("/initp");//转发的请求路径
// requestDispatcher.forward(req,resp);//调用forward实现请求转发
context.getRequestDispatcher("/initp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
rf com.dandelion.servlet.RfServlet rf /rf
4、读取资源文件
Properties
在Java目录下新建properties
在resources目录下新建properties
发现:都被打包到了同一个路径下:classes,称为classpath;
思路:需要一个文件流;
public class GetpServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(is);
String user = prop.getProperty("username");
String pwd = prop.getProperty("password");
resp.getWriter().print(user+":"+pwd);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
getp com.dandelion.servlet.GetpServlet getp /getp



