IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> 学习javaweb -> 正文阅读

[Java知识库]学习javaweb


前言


提示:以下是本篇文章正文内容,下面案例可供参考

一、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 {
		// TODO Auto-generated method stub
		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 {
		// TODO Auto-generated method stub
		
	}
	
}


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();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		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 {
		// TODO Auto-generated method stub
	}

}

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();
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setStatus(404);
		response.sendError(404,"页面丢失了");
		response.addHeader("Refresh", "2");
		response.addHeader("Refresh", "5;url=http://www.baidu.com");
		PrintWriter out=response.getWriter();
		//不能同时存在
		//ServletOutputStream out=response.getOutputStream();
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		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 {
		// TODO Auto-generated method stub
		response.addHeader("content-disposition", "inline;filename=404.jpg");
		BufferedImage image=new BufferedImage(400,300,BufferedImage.TYPE_INT_RGB);
		//ImageIO.write(image, "jpg", response.getOutputStream());
		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 {
		// TODO Auto-generated method stub
		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 {
        	// TODO Auto-generated method stub
        	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 {
        // TODO Auto-generated method stub
        	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>

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-10-07 13:43:18  更:2021-10-07 13:45:27 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 18:56:21-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码