tomcat配置:
可以添加多个war包,每个包要取不同的名字,注意/,反应到url里 http://localhost:8080/s1
web.xml配置
servelet写好后,要在web.xml中注册这个类,让其可用
是要注册的取的名字,和mapping对应就行
是要访问的url
可以在idea后台输出一些信息:
一旦访问了/hello1,就进入了doGet方法
mapping
ServletContext:
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();//this 使用同一个Context
String username = (String) context.getAttribute("username"); //得到Content中的数据
resp.setContentType("text/html"); //设置响应形式
resp.setCharacterEncoding("utf-8");
resp.getWriter().print("名字"+username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
注册,注意是两个
请求转发后,url不变,状态码仍是200
重定向相当于重新访问一url
pom.xml配置:
<build>
<resources>
<resource>
<!--输出目录,默认为/target/classes-->
<!-- <targetPath>${project.build.outputDirectory} </targetPath>-->
<!-- 设定主资源目录 -->
<directory>src/main/java</directory>
<!-- 只处理如下配置中包含的资源类型 -->
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<!-- 不处理如下配置中包含的资源类型(剔除下如下配置中包含的资源类型)-->
<excludes>
<exclude></exclude>
</excludes>
<!-- 是否对主资源目录开启资源过滤 -->
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<excludes>
<exclude></exclude>
</excludes>
<filtering>true</filtering>
</resource>
</resources>
</build>
获得网络文件数据:
前面设置了state为300,添加了一个header: a=b
面试:用buffer时,中文乱码问题怎么解决?
构建缓冲区还得再看看,取文件的名字还得再看看
怎么将图片打包到WEB-INF里? 在pom.xml里设置,就像properties一样
如何不用图片的绝对地址输出? 可以将图片放在resources,打包到WEN-INF里,再get到文件的输入流resourceAsStream
用resourceAsStream输入流当作fileInputStream,输出