Servlet
一. 创建servlet项目配置内容
- 新建web项目要记得在pom.xml中导入servlet包
<dependencies>
<!-- maven可以自动导入导入的jar包所依赖的其他jar-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<!-- <scope>provided</scope> //作用域(范围),这里保留idea会自动报错-->
</dependency>
</dependencies>
<!--在 build 中配置 resources,解决资源导出失败问题-->
<!--include 标签指定哪些文件将被匹配,以*作为通配符-->
<!--exclude 标签指定哪些文件将被忽略,以*作为通配符-->
<!--filtering 构建过程中是否对资源进行过滤,默认false-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
- class代码
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("hello");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
- web.xml代码
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.yorha.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
-
控制台输出
[2023-01-05 10:46:22,761] 工件 servlet-01:Web exploded: 正在部署工件,请稍候…
[2023-01-05 10:46:23,200] 工件 servlet-01:Web exploded: 工件已成功部署
[2023-01-05 10:46:23,200] 工件 servlet-01:Web exploded: 部署已花费 439 毫秒
hello
二. 解决服务器输出乱码
将
# java.util.logging.ConsoleHandler.encoding = GBK
改为
java.util.logging.ConsoleHandler.encoding = UTF-8
结果服务器没乱码了,但class文件输出还是乱码
破案了
18版的openjdk有毛病,重新下个17版就行了
三. ServletContext使用
3.1 先存储servletContext
- 直接上代码吧,先调类(可能,猜的)
- 定义变量
- 最后建立属性
ServletContext servletContext = this.getServletContext();
String name = "yorha";
servletContext.setAttribute("name",name);
3.2 调用servletContext
- 第一步一样的,先调类
- 然后定义变量
- 用getAttribute方法获取servletContext内容
- 因为setAttributet中存储的类型是object(数组),所以需要强制类型转化,代码如下
ServletContext servletContext = this.getServletContext();
String name = (String) servletContext.getAttribute("name");
网页回显代码
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().print("用户名:"+name);
未访问hello时get回显内容如下
访问hello文件再回访get回显如下
这里补充一下web.xml配置
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.yorha.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>get</servlet-name>
<servlet-class>com.yorha.servlet.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>get</servlet-name>
<url-pattern>/get</url-pattern>
</servlet-mapping>
四. getInitParameter获取初始化参数
4.1 基本配置
- web.xml配置内容
<!-- 配置web的一些初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
- class内容
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
resp.getWriter().print(url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
输出效果
4.2 转发
实现代码
ServletContext context = this.getServletContext();
/* RequestDispatcher requestDispatcher = context.getRequestDispatcher("/demo03");//转发路径
requestDispatcher.forward(req,resp);//调用servlet实现请求*/
// 简洁化
context.getRequestDispatcher("/demo03").forward(req,resp);
实例图
可以看到路径不同,但都显示demo03的内容
五. InputStream获取资源包
5.1 java代码
ServletContext context = this.getServletContext();
InputStream resourceAsStream = context.getResourceAsStream("/classes/db.properties");
Properties properties = new Properties();
properties.load(resourceAsStream);
String username = properties.getProperty("username");
String password = properties.getProperty("password");
System.out.println(username+":"+password);
5.2 路径
照着target目录来的
5.3 结果╥﹏╥
提示inStream parameter is null
5.4 inStream parameter is null解决办法
- 然后试着去改了pom.xml文件
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
改完发现没啥用
然后去修改java代码中的路径
InputStream resourceAsStream = context.getResourceAsStream("/WEB-INF/classes/db.properties");
添加WEB-INF目录后获取成功了
但很明显target目录下是没WEB-INF目录的,但如果看out文件中就能发现db.properties是在WEB-INF/classes中
标签:xml,Servlet,resp,req,properties,servlet,hello From: https://www.cnblogs.com/yorha/p/17029222.html