目的:编写一个ParamServlet类,读取web.xml
配置文件中的全局参数和局部参数,在游览器中显示
1、ParamServlet类
public class ParamServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context=getServletContext();
String s1=context.getInitParameter("cdu");
ServletConfig config=getServletConfig();
String s2=config.getInitParameter("major");
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer=resp.getWriter();
writer.println("cdu:"+s1);
writer.println("
");
writer.println("major:"+s2);
writer.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
注意使用resp.setContentType("text/html;charset=utf-8");
设置编码,不然游览器显示中文会乱码。
2、web.xml
文件:
<context-param>
<param-name>cduparam-name>
<param-value>我爱cduparam-value>
context-param>
<servlet>
<servlet-name>ParamServletservlet-name>
<servlet-class>com.qing.ParamServletservlet-class>
<init-param>
<param-name>majorparam-name>
<param-value>我在学webparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>ParamServletservlet-name>
<url-pattern>/paramurl-pattern>
servlet-mapping>
全局参数在
标签中配置
局部参数在
标签中配置
目的:编写PropServlet类,读取my.properties
文件中的数据,并使用数据生成一个图片,在游览器中显示。
1、my.properties资源文件
my.name=我爱javaWeb
my.id=2022.2.26
2、PropServlet类
@WebServlet("/prop")
public class PropServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html; charset=UTF-8");
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/my.properties");
Properties prop = new Properties();
prop.load(is);
String name = prop.getProperty("my.name");
System.out.println(name);
String id = prop.getProperty("my.id");
//在内存中创建一个图片
BufferedImage image = new BufferedImage(500, 70, BufferedImage.TYPE_INT_RGB);
//得到图片
Graphics2D g = (Graphics2D) image.getGraphics();
//设置图片背景颜色
g.setColor(Color.white);
g.fillRect(0, 0, 300, 50);
//给图片写数据
g.setColor(Color.red);
g.setFont(new Font(null, Font.BOLD, 20));
g.drawString(name, 0, 20);
g.drawString(id, 0, 40);
//告诉浏览器,这个请求用图片的方式打开
resp.setContentType("image/jpg");
//把图片写给浏览器
ImageIO.write(image, "jpg", resp.getOutputStream());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
my.properties
文件的路径如下,maven项目中的resources
文件夹是放在与java代码同级目录下的,如果想要修改,则需在web.xml
中配置。
方式一: ClassLoader 获取项目根路径
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("my.properties");
使用 classLoader 得到的路径是 项目实际前缀路径是/WEB-INF/classes/
,注意文件名前面没有 /
方式二 : ServletContext 获取项目根路径
InputStream stream = this.getServletContext().getResourceAsStream("/WEB-INF/classes/my.properties");
使用 ServletContext 获取的项目路径就是项目路径,路径前面必须加 /
在上面的读取my.properties
中的数据时,读取文件中的中文,会显示乱码。解决方案是file->setting
打开设置。输入File Encodings
,勾选UTF-8编码。如果不起作用,则删掉properties文件,重新创建就可,我是重新创建后解决的。