父项目中会有
servlet-01
子项目会有
javaweb-02-servlet
com.kuang
1.0-SNAPSHOT
父项目中的java子项目可以直接使用son extends father
package com.yues.web01;
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 helloo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter(); //响应流
writer.print("Hello,Serlvet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
为什么需要映射:我们写的是JAVA程序,但是要通过浏览器访问,而浏览器需要连接web服务器,所以我们需要再web服务中注册我们写的Servlet,还需给他一个浏览器能够访问的路径
hello
com.kuang.servlet.HelloServlet
hello
/hello
Servlet是由Web服务器调用,web服务器在收到浏览器请求之后,会:
一个Servlet可以指定一个映射路径
hello
/hello
一个Servlet可以指定多个映射路径
hello
/hello
hello
/hello2
hello
/hello3
hello
/hello4
hello
/hello5
一个Servlet可以指定通用映射路径
hello
/hello/*
默认请求路径
hello
/*
指定一些后缀或者前缀等等….
hello
*.qinjiang
优先级问题
指定了固有的映射路径优先级最高,如果找不到就会走默认的处理请求;
error
com.kuang.servlet.ErrorServlet
error
/*
web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用
什么是共享数据呢?就是说,在一个Servlet中保存的数据,可以在另一个Servlet中拿到
public class contextstuday extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
servletContext.setAttribute("name","小可爱");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
public class getcontext extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
ServletContext context = this.getServletContext();
String name = (String) context.getAttribute("name");
resp.getWriter().print(name);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
Servlet的映射
three
com.yues.web01.contextstuday
three
/three
four
com.yues.web01.getcontext
four
/four
public class getinit extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String flowername = servletContext.getInitParameter("flowername");
resp.getWriter().print(flowername);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
flowername
beautiful
five
com.yues.web01.getinit
five
/five
这个图是说:比如A给B发送了一个请求,但是B将这个请求交给了C去处理,然后C再响应给B,B再响应给A,这个过程就是请求转发,在这个过程中,地址栏的url路径不发生改变,在A发送请求再到A接受到响应的过程中,A始终是与B进行交互的
public class achieve extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
servletContext.getRequestDispatcher("/one").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
six
com.yues.web01.achieve
six
/six
public class readfile extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
InputStream in = servletContext.getResourceAsStream("/WEB-INF/classes/ff.properties");
Properties properties = new Properties();
properties.load(in);
String username = properties.getProperty("username");
String password = properties.getProperty("password");
resp.getWriter().print(username+"\t"+password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
public class judge extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String realPath = "E:\\javaweb-servlet\\servlet01\\src\\main\\resources\\aaa.png";
System.out.println("下载文件的路径:"+realPath)
String fileName = realPath.substring(realPath.lastIndexOf("\\") + 1);
resp.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"UTF-8");
ileInputStream in = new FileInputStream(realPath);
int len = 0;
byte[] buffer = new byte[1024];
ServletOutputStream out = resp.getOutputStream();
while ((len=in.read(buffer))>0){
out.write(buffer,0,len);
}
in.close();
out.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
two
com.yues.web01.judge
two
/two