JavaWeb01_Servlet

JavaWeb01_Servlet


Entry project:

Create a Maven project and select the webapp template.

rely:

<dependencies>
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>javax.servlet-api</artifactId>
		<version>4.0.1</version>
	</dependency>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>3.8.1</version>
		<scope>test</scope>
	</dependency>
</dependencies>
package com.blu.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 HelloServlet extends HttpServlet {
    
    

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    
    
		response.setContentType("text/html");
		//java默认的字符编码是ISO-8859-1,中文会乱码!
		response.setCharacterEncoding("utf-8");
		
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<head>");
		out.println("<title>Hello BLU!</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>你好 BLU!</h1>");
		out.println("</body>");
		out.println("</html>");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
		super.doPost(req, resp);
	}

}

web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<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"
         metadata-complete="true">
  
  <servlet>
  	<servlet-name>helloServlet</servlet-name>
  	<servlet-class>com.blu.servlet.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>helloServlet</servlet-name>
  	<url-pattern>/hello</url-pattern>
  </servlet-mapping>
  
</web-app>

Create header.html in the webapp directory:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>标题1</h1>
<p>Hi,BLU!</p>
</body>
</html>

start up

Request http://localhost:8080/javawebdemo , visit the index.jsp created by default in the webapp directory.
Request http://localhost:8080/javawebdemo/header.html to access the header.html we created.
Request http:/ /localhost:8080/javawebdemo/hello can access the Servlet we wrote

Insert picture description here

Servlet

Servlet is a technology used by Sun to develop dynamic web. It is an interface.
If you want to develop a Servlet program, you only need the following two steps:

  • Write a class to implement the Servlet interface
  • Deploy the developed java classes to the web server

Insert picture description here

ServletContext

The ServletContext object represents the current Web application and can be used for data sharing and communication between Servlets

  • AServlet: Set the value in ServletContext
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    
    
	
	response.setContentType("text/html");
	response.setCharacterEncoding("utf-8");
	ServletContext servletContext = this.getServletContext();
	servletContext.setAttribute("username", "BLU");
		
}
  • BServlet: take value from ServletContext
@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
		
	resp.setContentType("text/html");
	resp.setCharacterEncoding("utf-8");
	
	ServletContext servletContext = this.getServletContext();
	String username = (String) servletContext.getAttribute("username");
	resp.getWriter().print("名字:"+username);
}

Note: You need to request AServlet to assign a value to username, and then request BServlet to get the value, otherwise the value is empty.


ServletContext application- set web initialization parameters:

<context-param>
	<param-name>url</param-name>
	<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
	ServletContext servletContext = this.getServletContext();
	String url = servletContext.getInitParameter("url");
	System.out.println(url);
}

Application of ServletContext -to achieve request forwarding:

  • AServlet :
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
	System.out.println("进入AServlet");
	ServletContext servletContext = this.getServletContext();
	RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/b");
	requestDispatcher.forward(req, resp);
}
  • BServlet :
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
	System.out.println("进入BServlet");
	resp.setContentType("text/html");
	resp.setCharacterEncoding("utf-8");
	PrintWriter out = resp.getWriter();
	out.println("Welcome to BServlet");	
}

Insert picture description here

Guess you like

Origin blog.csdn.net/BLU_111/article/details/108643025