开发一个Servlet程序
编写一个类实现servlet接口
把开发好的Java类部署到Web服务器中
servlet 在sun公司有两个默认的实现类:HttpServlet,
HelloServlet 直接继承HttpServlet
Servlet接口 service
GenericServlet service
HttpServlet service重写了
自己的类
重写 doGet doPost
由于get或post只是请求实现的不同的方式,可以相互调用,业务逻辑都一样
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//ServletOutputStream outputStream = resp.getOutputStream();
PrintWriter writer = resp.getWriter();//响应流
writer.print(“Hello,Servlet”);
}
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPut(req, resp);
}
}
编写一个Servlet的映射 在web.xml中
我们写的java程序,但是需要通过浏览器访问,而浏览器需要连接Web服务器,所以我们需要在web服务中注册我们写的Servlet,还需要一个浏览器能够访问到的url路径
编写servlet映射
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.zxw.servlet.HelloServlet</servlet-class>
</servlet>
<!--servlet请求路径-->
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
servlet与servletMapping 是成对出现的
配置Tomcat
Mapper
一个servlet可以指定一个映射路径
hello
/hello
一个servlet可以指定多个映射路径
hello
/hello
hello
/hello/hello
一个servlet可以指定通用映射路径
hello
/hello/
默认请求路径
hello
/
可以自定义后缀实现请求映射
前面不能加项目映射的路径
hello
.zxw
优先级问题
制定了固有的映射路径优先级最高,如果找不到就会走默认的处理请求
ServletContext 上下文 (中间件) 可以保存一些东西
web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的Web应用
1.共享数据
两个servlet的数据可以共享
创建一个放置数据的类
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//this.getInitParameter();初始化参数
// this.getServletContext();Servlet配置
// this.getServletConfig();Servlet上下文
ServletContext servletContext = this.getServletContext();
String username = "张馨文";
servletContext.setAttribute("username",username);//将一个数据保存在ServletContext中,名字为username
System.out.println("hello");
}
}
创建一个接收数据的类
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String username = (String)servletContext.getAttribute("username");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().print("名字"+username);
}
配置web.xml
hello
com.zxw.servlet.HelloServlet
hello
/hello
getc
com.zxw.servlet.GetServlet
getc
/getc
测试访问结果
数据放在上 响应放在下
请求转发
RequsetDispatch
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/gp");//转发的请求路径
requestDispatcher.forward(req,resp);//调用forword实现请求转发
}
读取资源文件 properties
注意资源过滤问题
1.创建一个Servlet类继承HttpServlet
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(is);
String username = prop.getProperty(“username”);
String password = prop.getProperty(“password”);
resp.getWriter().print(username + password);