参考下文:
Eclipse创建web项目,WebContent 修改为WebRoot
1.将下个页面中的Default output folder 更改成WbRoot\WEB-INF\classes然后点击next
2.将下面页面的Content directory里面的内容改为webroot并且将自动生成web.xml文件勾中。
1. 全路径匹配
以 / 开始,如: /a /aa/bb
localhost:8080/项目名称/aa/bb
如:
<servlet-mapping>
<servlet-name>MyRegister</servlet-name>
<url-pattern>/MyRegister</url-pattern>
</servlet-mapping>
浏览器地址:
http://localhost:8080/EX05Request/MyRegister
2. 路径匹配 , 前半段匹配
以 / 开始 , 但是以 * 结束 /a/* /*
*号其实是一个通配符,匹配任意文字
localhost:8080/项目名称/aa/bb
<servlet-mapping>
<servlet-name>MyRegister</servlet-name>
<url-pattern>/*
浏览器地址:
http://localhost:8080/EX05Request/gfgs
3. 以扩展名匹配
写法: 没有/ ,以 * 开始 *.扩展名 *.aa *.bb
<servlet-mapping>
<servlet-name>MyRegister</servlet-name>
<url-pattern>*mypath</url-pattern>
</servlet-mapping>
浏览器地址:
http://localhost:8080/EX05Request/hellopath
Servlet 上下文
每个web工程都只有一个ServletContext对象。 说白了也就是不管在哪个servlet里面,获取到的这个类的对象都是同一个。
1、web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>EX05Request</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置全局参数 -->
<context-param>
<param-name>address</param-name>
<param-value>北京市海淀区</param-value>
</context-param>
<servlet>
<servlet-name>MyRegister</servlet-name>
<servlet-class>com.fengray.Request.MyRegister</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyRegister</servlet-name>
<url-pattern>/MyRegister</url-pattern>
</servlet-mapping>
</web-app>
2、servlet文件MyRegister.java
package com.fengray.Request;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyRegister extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取context对象
ServletContext context=getServletContext();
//配置参数,获取context的初始对象
String addString=context.getInitParameter("address");
System.out.println("address:"+addString);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
结果为:
address:北京市海淀区
1.可以获取全局配置参数
获取全局参数
2.可以获取Web应用中的资源
src目录下创建jianconfig.properties,输入键值对
name=“zhangsan”
servlet文件 MyRegister
package com.fengray.Request;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyRegister extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//创建属性对象
Properties properties=new Properties();
//指定载入的数据源
//这里的classes是发布到服务器上的目录,即tomcat里的bin目录,需要将该目录拷贝到tomcat的bin目录中
InputStream inputStream=new FileInputStream("classes/config.properties");
properties.load(inputStream);
//获得name属性的值
String nameString=properties.getProperty("name");
System.out.println("name:"+nameString);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
结果:
总是报文件未找到错误(properties)文件未找到
除MyRegister.java文件外同上例
MyRegister.java
package com.fengray.Request;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyRegister extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取servletcontext对象
ServletContext context=getServletContext();
String path=context.getRealPath("file/jianconfig.properties");
System.out.println("path:"+path);
//创建属性对象
Properties properties=new Properties();
//指定载入的数据源
//这里的classes是发布到服务器上的目录,即tomcat里的bin目录,需要将该目录拷贝到tomcat的bin目录中
InputStream inputStream=new FileInputStream(path);
properties.load(inputStream);
//获得name属性的值
String nameString=properties.getProperty("name");
System.out.println("name:"+nameString);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
结果是:
path:E:\EclipseWorkSpace.metadata.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\EX05Request\file\jianconfig.properties
name:“zhangsan”
在上例的基础上修改servlet文件
package com.fengray.Request;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyRegister extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
mytest01();
}
/**
* @throws FileNotFoundException
* @throws IOException
*/
private void mytest01() throws FileNotFoundException, IOException {
//获取servletcontext对象
ServletContext context=getServletContext();
//创建属性对象
Properties properties=new Properties();
//指定载入的数据源
//获取资源转换为流对象
InputStream inputStream=context.getResourceAsStream("file/jianconfig.properties");
properties.load(inputStream);
//获得name属性的值
String nameString=properties.getProperty("name");
System.out.println("name:"+nameString);
inputStream.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
结果为:
name:“zhangsan”
在servers中双击打开,确保下图红框显示
参考文章:
解决eclipse中web项目部署至Tomcat在Tomcat安装目录下找不到项目问题
全选代码片段—右键----refactor—extract method—填入方法名即可
利用eclipse抽取 代码片段为方法
之前例的基础上修改MyRegister.java文件
package com.fengray.Request;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyRegister extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
mytest01();
}
/**
* @throws FileNotFoundException
* @throws IOException
*/
private void mytest01() throws FileNotFoundException, IOException {
//获取servletcontext对象
ServletContext context=getServletContext();
//创建属性对象
Properties properties=new Properties();
//获取该java文件的class,然后获取到加载这个class到虚拟机中的那个类加载对象
//指定载入的数据源
//获取资源转换为流对象
//路径使用tomcat里的目录这里实际是项目路径/WEB-INF/Classes/file/jianconfig.properties,因此要上跨两级目录
InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("../../file/jianconfig.properties");
properties.load(inputStream);
//获得name属性的值
String nameString=properties.getProperty("name");
System.out.println("name:"+nameString);
inputStream.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
结果:
name:“zhangsan”
1.创建login.html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<H2>请输入一下内容完成登录</H2>
<form method="get" action="LoginServelet">
账号:<input type="text" name="username" hint="urname"/><br/>
密码:<input type="password" name="password" hint="ur password"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
2.创建LoginServelet文件并继承HttpServlet
package com.fengray.Request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Administrator
*
*/
public class LoginServelet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取数据
String usernameString=req.getParameter("username");
String passwordString=req.getParameter("password");
System.out.println("username:"+usernameString+" password:"+passwordString);
//校验数据
if ("admin".equals(usernameString) && "123".equals(passwordString)) {
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3、注册web.xml
略
结果:
username:admin password:123
登录成功
主要在上例的基础上,改动LoginServlet.java文件
package com.fengray.Request;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Administrator
*req:即request,用户请求的信息
*resp:即response,服务器响应浏览器的信息
*/
public class LoginServelet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取数据
String usernameString=req.getParameter("username");
String passwordString=req.getParameter("password");
System.out.println("username:"+usernameString+" password:"+passwordString);
//校验数据
PrintWriter printWriter=resp.getWriter();
if ("admin".equals(usernameString) && "123".equals(passwordString)) {
printWriter.write("login success");
}else {
printWriter.write("登录失败");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
1、在WebRoot目录下创建loginSuccess.html文件,作为成功后跳转的页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录成功</title>
</head>
<body>
<h2>登录成功</h2>
</body>
</html>
2.上例的基础上修改LoginServlet.java
package com.fengray.Request;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Administrator
*req:即request,用户请求的信息
*resp:即response,服务器响应浏览器的信息
*/
public class LoginServelet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取数据
String usernameString=req.getParameter("username");
String passwordString=req.getParameter("password");
System.out.println("username:"+usernameString+" password:"+passwordString);
//校验数据
PrintWriter printWriter=resp.getWriter();
if ("admin".equals(usernameString) && "123".equals(passwordString)) {
//设置状态码,状态码为 重新定位 状态码
resp.setStatus(302);
//定位跳转的位置是哪个页面
resp.setHeader("Location", "loginSuccess.html");
}else {
printWriter.write("登录失败");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
上例基础上修改LoginServlet.java
package com.fengray.Request;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Administrator
*req:即request,用户请求的信息
*resp:即response,服务器响应浏览器的信息
*/
public class LoginServelet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取数据
String usernameString=req.getParameter("username");
String passwordString=req.getParameter("password");
System.out.println("username:"+usernameString+" password:"+passwordString);
//校验数据
PrintWriter printWriter=resp.getWriter();
if ("admin".equals(usernameString) && "123".equals(passwordString)) {
//设置状态码,状态码为 重新定位 状态码
resp.setStatus(302);
//成功的次数累加
int totalCount=0;
//取以前存的值,在旧基础值上+1
Object object=getServletContext().getAttribute("count");
if (object!=null) {
totalCount=(int) object;
}
//给这个count赋新值
getServletContext().setAttribute("count", totalCount+1);
System.out.println("登录成功的次数是:"+totalCount);
//定位跳转的位置是哪个页面
resp.setHeader("Location", "loginSuccess.html");
}else {
printWriter.write("登录失败");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
结果是:
username:admin password:123
登录成功的次数是:0
username:admin password:123
登录成功的次数是:1
在上例的基础上:
1.loginsuccess.html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录成功</title>
</head>
<body>
<h2>登录成功</h2>
<a href="CountServlet" target="self">获取网站登录成功总数</a>
</body>
</html>
2.countServlet
package com.fengray.Request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Administrator
*
*/
public class CountServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//取值
int count=(int) getServletContext().getAttribute("count");
//输出到界面
resp.getWriter().write("sucecessful login "+count+" times");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3.LoginServelet.java
package com.fengray.Request;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Administrator
*req:即request,用户请求的信息
*resp:即response,服务器响应浏览器的信息
*/
public class LoginServelet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取数据
String usernameString=req.getParameter("username");
String passwordString=req.getParameter("password");
System.out.println("username:"+usernameString+" password:"+passwordString);
//校验数据
PrintWriter printWriter=resp.getWriter();
if ("admin".equals(usernameString) && "123".equals(passwordString)) {
//设置状态码,状态码为 重新定位 状态码
resp.setStatus(302);
//成功的次数累加
int totalCount=0;
//取以前存的值,在旧基础值上+1
Object object=getServletContext().getAttribute("count");
if (object!=null) {
totalCount=(int) object;
}
//给这个count赋新值
getServletContext().setAttribute("count", totalCount+1);
System.out.println("登录成功的次数是:"+totalCount);
//定位跳转的位置是哪个页面
resp.setHeader("Location", "loginSuccess.html");
}else {
printWriter.write("登录失败");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
4、在web.xml文件中配置CountServlet
服务器启动的时候,会为托管的每一个web应用程序,创建一个ServletContext对象
从服务器移除托管,或者是关闭服务器。
只要在这个项目里面,都可以取。 只要同一个项目。 A项目 存, 在B项目取,是取不到的? ServletContext对象不同。
这个对象封装了客户端提交过来的一切数据。
RequestEnum.java文件
public class RequestEnum extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Enumeration<String> headerNames=req.getHeaderNames();
while (headerNames.hasMoreElements()) {
String name= (String) headerNames.nextElement();
System.out.println("name:"+name);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
结果
name:host
name:user-agent
name:accept
name:accept-language
name:accept-encoding
name:connection
name:upgrade-insecure-requests
输出Header的名值对
package com.fengray.Request;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Administrator
*
*/
public class RequestEnum extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Enumeration<String> headerNames=req.getHeaderNames();
while (headerNames.hasMoreElements()) {
String name= (String) headerNames.nextElement();
String valueString=req.getHeader(name);
System.out.println(name+"="+valueString);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
结果是:
host=localhost:8080
user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0
accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
accept-language=zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
accept-encoding=gzip, deflate
connection=keep-alive
upgrade-insecure-requests=1
RequestEnum .java文件
package com.fengray.Request;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Administrator
*
*/
public class RequestEnum extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取客户端提交上来的数据
String nameString=req.getParameter("name");
String addressString=req.getParameter("address");
System.out.println("name:"+nameString);
System.out.println("address:"+addressString);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
浏览器输入:
结果:
name:张三
address:lisi
RequestEnum.java类
package com.fengray.Request;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Administrator
*
*/
public class RequestEnum extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取客户端提交上来的数据
String nameString=req.getParameter("name");
String addressString=req.getParameter("address");
System.out.println("name:"+nameString);
System.out.println("address:"+addressString);
System.out.println("*************");
Map<String, String[]> map=req.getParameterMap();//使用的事java.util.Map包
Set<String> keySet=map.keySet();
Iterator<String> iterator=keySet.iterator();
while (iterator.hasNext()) {
String keyString = (String) iterator.next();
String valueString=map.get(keyString)[0];
System.out.println(keyString+"--"+valueString);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
name:张三
address:beijing
name–张三
address–beijing
客户端提交数据给服务器端,如果数据中带有中文的话,有可能会出现乱码情况,那么可以参照以下方法解决。
如果是GET方式
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("userName="+username+"==password="+password);
//get请求过来的数据,在url地址栏上就已经经过编码了,所以我们取到的就是乱码,
//tomcat收到了这批数据,getParameter 默认使用ISO-8859-1去解码
//先让文字回到ISO-8859-1对应的字节数组 , 然后再按utf-8组拼字符串
username = new String(username.getBytes("ISO-8859-1") , "UTF-8");
System.out.println("userName="+username+"==password="+password);
直接在tomcat里面做配置,以后get请求过来的数据永远都是用UTF-8编码。
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
如果是POST方式
这个说的是设置请求体里面的文字编码。 get方式,用这行,有用吗? —> 没用
request.setCharacterEncoding(“UTF-8”);
这行设置一定要写在getParameter之前。
不管是字节流还是字符流,直接使用一行代码就可以了。
response.setContentType(“text/html;charset=UTF-8”);
然后在写数据即可。
package com.fengray.Request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javafx.geometry.Side;
import sun.nio.cs.ext.ISCII91;
public class MyResponse extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置一个头,规定浏览器看这份数据的时候,使用什么编码来看
resp.setHeader("Content-Type","text/html;charset=UTF-8");
//以字符流的方式写数据
resp.getWriter().write("this is my response
");
//以字节流的方式来写数据
resp.getOutputStream().write("我爱北京天安门
".getBytes());//这个方法只接收byte数组
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
1. 直接以超链接的方式下载,不写任何代码。 也能够下载东西下来。
让tomcat的默认servlet去提供下载:
<a href="download/aa.jpg">aa.jpg</a><br>
<a href="download/bb.txt">bb.txt</a><br>
<a href="download/cc.rar">cc.rar</a><br>
2. 手动编码的方式。
web文件
<a href="项目名称?filename=aa.jpg">aa.jpg</a><br>
<a href="MyResponse /bb.txt">bb.txt</a><br>//MyResponse 是在web.xml中指定的MyResponse的映射路径
<a href="download/cc.rar">cc.rar</a><br>
servlet文件
package com.fengray.Request;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javafx.geometry.Side;
import sun.nio.cs.ext.ISCII91;
/**
* @author Administrator
*
*/
public class MyResponse extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取要下载的文件名字
String filename=req.getParameter("filename");
//获取这个文件在tomcat中的绝对路径地址
String path=getServletContext().getRealPath("download/"+filename);//这里的download为webRoot下的下载资源文件夹的名字
//设置浏览器以下载的方式提示用户,而不是直接展示
resp.setHeader("Content-Disposition","attachment;filename="+filename);
//转化为输入流
InputStream inputStream=new FileInputStream(path);
OutputStream os=resp.getOutputStream();
int len=0;
byte[] buffer=new byte[1024];
while ((len=inputStream.read(buffer))!=-1) {
os.write(buffer,0,len);
}
os.close();
inputStream.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
重定向
之前的写法
response.setStatus(302);
response.setHeader("Location", "login_success.html");*/
//重定向写法: 重新定位方向 参数即跳转的位置
response.sendRedirect("login_success.html");
地址上显示的是最后的那个资源的路径地址
请求次数最少有两次, 服务器在第一次请求后,会返回302 以及一个地址, 浏览器在根据这个地址,执行第二次访问。
可以跳转到任意路径。 不是自己的工程也可以跳。
效率稍微低一点, 执行两次请求。
后续的请求,没法使用上一次的request存储的数据,或者 没法使用上一次的request对象,因为这是两次不同的请求。
请求转发
//请求转发的写法: 参数即跳转的位置
request.getRequestDispatcher("login_success.html").forward(request, response);