每个访问的web.xml配置
<servlet>
<servlet
-name
>helloSeerlet
</servlet
-name
>
<servlet
-class>com
.servlet
.HelloServlet
</servlet
-class>
</servlet
>
<servlet
-mapping
>
<servlet
-name
>helloSeerlet
</servlet
-name
>
<url
-pattern
>/hello
</url
-pattern
>
</servlet
-mapping
>
输出消息
@Override
protected void doGet(HttpServletRequest req
, HttpServletResponse response
) throws ServletException
, IOException
{
ServletContext servletContext
= this.getServletContext();
response
.setCharacterEncoding("UTF-8");
response
.setContentType("text/html");
String name
= "zzh";
System
.out
.println("zzh");
servletContext
.setAttribute("name",name
);
}
get文件传输
@Override
protected void doGet(HttpServletRequest req
, HttpServletResponse response
) throws ServletException
, IOException
{
String realPath
= "E:\\IntelliJ IDEA 2019.3.3\\project\\javaweb\\Javaweb-01-maven\\target\\classes\\1.png";
System
.out
.println("下载的路径"+realPath
);
String fileName
= realPath
.substring(realPath
.lastIndexOf("\\")+1);
response
.setHeader("Content-Disposition","attachment;fileName="+fileName
);
FileInputStream in
= new FileInputStream(realPath
);
int len
= 0;
byte[] buffer
= new byte[1024];
ServletOutputStream out
= response
.getOutputStream();
while((len
= in
.read(buffer
))>0){
out
.write(buffer
,0,len
);
}
out
.close();
in
.close();
}