前言
提示:以下是本篇文章正文内容,下面案例可供参考
一、Application1Servlet
package javaweb.servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
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 Application1Servlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
ServletContext application=getServletContext();
application=request.getServletContext();
out.println("<ul>");
Enumeration<String> e=application.getInitParameterNames();
while(e.hasMoreElements()){
String name=e.nextElement();
String value=application.getInitParameter(name);
out.println(String.format("<li><strong>%s,</strong>%s</li>",name,value));
}
out.println("</ul>");
Integer count=(Integer)application.getAttribute("count");
if(count==null){
count=0;
}
count++;
application.setAttribute("count", count);
out.print(String.format("<h1>网站共访问%d次</h1>", count));
String path=application.getRealPath("/images");
out.print(String.format("<p>%s</p>", path));
out.print("<ul>");
for(File i : new File(path).listFiles()){
out.print(String.format("<li>[%s]%s</li>", i.isFile()?"文件":"文件夹",i.getName()));
}
out.print("</ul>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
2.实现效果
二、Application25Servlet
代码如下(示例):
package javaweb.servlet;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Application25Servlet")
public class Application25Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public Application25Servlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
Properties prop=new Properties();
String path=getServletContext().getRealPath("/WEB-INF/classes/app.properties");
prop.load(new FileInputStream(path));
out.print("<ul>");
out.print(String.format("<li><strong>%s:</strong>%s</li>",
"app.title",
prop.getProperty("app.title")));
out.print(String.format("<li><strong>%s:</strong>%s</li>",
"app.course",
prop.getProperty("app.course")));
out.print("</ul>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
2.实现效果
三 、Response1Servlet
代码如下(示例):
package javaweb.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Response1Servlet")
public class Response1Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public Response1Servlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setStatus(404);
response.sendError(404,"页面丢失了");
response.addHeader("Refresh", "2");
response.addHeader("Refresh", "5;url=http://www.baidu.com");
PrintWriter out=response.getWriter();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
2.实现效果
四、Response2Servlet
package javaweb.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Response2")
public class Response2Servlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.addHeader("content-disposition", "inline;filename=404.jpg");
BufferedImage image=new BufferedImage(400,300,BufferedImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
g.setColor(Color.GREEN);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.setColor(Color.RED);
g.drawRect(0, 0, image.getWidth()/2, image.getHeight());
g.setColor(new Color(255,255,255,70));
g.setFont(new Font("黑体", Font.BOLD, 60));
g.drawString("武汉城市学院", 20, 100);
ImageIO.write(image, "jpg", response.getOutputStream());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
2.实现效果
五、ServletAServlet
package javaweb.servlet;
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;
public class ServletAServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charst=utf-8");
PrintWriter out=response.getWriter();
out.print("<html>");
out.print("<head>");
out.print("<title>boy</title>");
out.print("</head>");
out.print("<body>");
out.print("<img src=\"images\\boy.jpg\">");
out.print("</body>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charst=utf-8");
PrintWriter out=response.getWriter();
out.print("<html>");
out.print("<head>");
out.print("<title>girl</title>");
out.print("</head>");
out.print("<body>");
out.print("<img src=\"images\\girl.jpg\">");
out.print("</body>");
}
}
2.实现效果
六、app.properties
app.title=\u6B66\u6C49\u57CE\u5E02\u5B66\u9662
app.course=web\u6280\u672F
七、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>javaweb</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>course</param-name>
<param-value>web技术</param-value>
</context-param>
<context-param>
<param-name>year</param-name>
<param-value>2021</param-value>
</context-param>
<servlet>
<servlet-name>Application1Servlet</servlet-name>
<servlet-class>javaweb.servlet.Application1Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Application1Servlet</servlet-name>
<url-pattern>/application1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ServletAServlet</servlet-name>
<servlet-class>javaweb.servlet.ServletAServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletAServlet</servlet-name>
<url-pattern>/servleta</url-pattern>
</servlet-mapping>
</web-app>
|