2019年7月14日。
这是我学习JavaWeb的第二天。
这一天,我学到了以下的知识。
Servlet接口被SUN公司定义了两个默认实现类,分别为:GenericServlet、HttpServlet
package servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ServletTest01 extends javax.servlet.http.HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
}
}
在web.xml文件中,会多出一个Servlet标签,代码如下所示:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>ServletTest01servlet-name>
<servlet-class>servlet.ServletTest01servlet-class>
servlet>
<servlet-mapping>
<servlet-name>ServletTest01servlet-name>
<url-pattern>/test01url-pattern>
servlet-mapping>
web-app>
这样,一个Servlet就创建完成了。
将这个请求,转换到另一个地址。
例如,我们请求 http://localhost:8080/javaweb/ServletTest01 却跳转到了 百度的首页,代码如下(ServletTest01对应的请求URL在web.xml提前设置好):
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "ServletTest01")
public class ServletTest01 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect("https://www.baidu.com/");
}
}
Web容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前的WEB应用。
ServletContext类,作用如下:
假设有一个名为ServletDemo01的Servlet,要传输一个键为name而值为abc的数据给ServletDemo02,代码如下:
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ServletDemo01 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
// 通过servlet对象,可以获取一个servletContext;
response.setCharacterEncoding("gbk");
ServletContext servletContext = this.getServletContext();
String username = "abc"; // 可以通过JDBC去读取;
// setAttribute:设置一个属性;(属性名,属性值)
servletContext.setAttribute("name",username);
response.getWriter().print("设置信息成功!" + username);
}
}
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ServletDemo02 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String name = (String) servletContext.getAttribute("name");
response.setCharacterEncoding("gbk");
response.getWriter().print("获取到的信息为:" + name );
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
1.在项目src目录下新建resources目录,在该目录下新建一个properties文件,如下所示:
driver=com.mysql.jdbc.Driver
username=root
password=123456
url=jdbc:mysql://localhost:3306/smbms
2.编写Servlet类
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
//读取properties配置文件
public class ServletDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//读取配置文件
//1.获得配置文件的路径
String realPath = this.getServletContext().getRealPath("/WEB-INF/classes/resources/database.properties");
System.out.println("取得的路径为:"+realPath);
Properties properties = new Properties();
FileInputStream is = new FileInputStream(realPath);
properties.load(is);//把文件流加载到配置文件的对象中;
String driver = properties.getProperty("driver");
String username = properties.getProperty("username");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
//响应到网页
resp.getWriter().println(driver);
resp.getWriter().println(username);
resp.getWriter().println(password);
resp.getWriter().println(url);
//=======================================
System.out.println(driver);
System.out.println(username);
System.out.println(password);
System.out.println(url);
}
}
3.配置web.xml
<servlet>
<servlet-name>Demo03servlet-name>
<servlet-class>com.servlet.ServletDemo03servlet-class>
servlet>
<servlet-mapping>
<servlet-name>Demo03servlet-name>
<url-pattern>/cccurl-pattern>
servlet-mapping>
4.访问查看
在页面的链接栏中输入 localhost:8080/demo02/ccc,可以查看到如下页面:
设置一个六位数的随机数作为验证码,每次刷新时验证码都会发生变换,代码如下:
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;
//验证码
public class ServletDemo04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//resp.getWriter().println(newRandom());
//自动刷新网页
resp.setHeader("refresh","2");
//验证码是一个图片 , 我们需要制作一个图片
BufferedImage image = new BufferedImage(100,30,BufferedImage.TYPE_3BYTE_BGR);
//图片写入一些东西
Graphics2D graphics = image.createGraphics();
graphics.setColor(Color.red);
String num = String.valueOf(newRandom());
graphics.drawString(num,10,10);
//想办法让浏览器知道我们给的是一张图片
resp.setContentType("image/jpg");
//让网站去打开图片
ImageIO.write(image,"jpg",resp.getOutputStream());
}
//生成随机数
public int newRandom(){
int num = (int)(Math.random()*9+1)*100000;
return num;
}
/*
@Test
public void test(){
ServletDemo04 servletDemo04 = new ServletDemo04();
for (int j = 0; j < 100; j++) {
int i = servletDemo04.newRandom();
System.out.println(i);
}
}
*/
}