Web Page Development Foundation: Servlet Technology

asdsServlet is an extension branch written in Java language that is applied to the Web server side. It precedes JSP and handles HTTP requests in Web applications conveniently. In the development of Java Web programs, Servlet is mainly applied to a variety of business logic, it has more business logic layer meaning than JSP, and Servlet securityExtensibility and performance are excellent, and it plays an extremely important role in Java Web program development and MVC mode application.

asdsServlet is a java application running on the Web server, which is written in ava language and has the advantages of java language. The difference between java programs is that the Servlet object mainly encapsulates the processing of HTTP requests, and its operation requires the support of Servlet container. In Java Web applications, the application of Servlet plays an important role. It plays an important role in Web applications.Request processing is also very powerful.

Asdsadadsdssdasdasadasdasdasdasdasadassdasdasadasdasdsadasdsadassadasdasdasdasdas --- JavaWeb From Getting Started to Proficiency

HttpServletRequest class

What does the HttpServletRequest class do?

Every time ssdss requests enter the Tomcat server, the Tomcat server parses the HTTP protocol information requested and encapsulates it into the Request object. It then passes it to the service method (doGet and doPost) for us to use. We can get all the requested information through the HttpServletRequest object.

Common methods of HttpServletRequest class

How to get request parameters

ssdss form:

<body> 
	<form action="http://localhost:8080/07_servlet/parameterServlet" method="get"> 
		User name:<input type="text" name="username"><br/> 
		Password:<input type="password" name="password"><br/> 
		Hobby:
		<input type="checkbox" name="hobby" value="cpp">C++ 
		<input type="checkbox" name="hobby" value="java">Java 
		<input type="checkbox" name="hobby" value="js">JavaScript<br/> 
		<input type="submit"> 
	</form> 
</body>

ssdssJava code:

public class ParameterServlet extends HttpServlet { 
@Override 
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
	// Get Request Parameters 
	String username = req.getParameter("username"); 
	String password = req.getParameter("password"); 
	String[] hobby = req.getParameterValues("hobby"); 
	System.out.println("User name:" + username); 
	System.out.println("Password:" + password); 
	System.out.println("Hobby:" + Arrays.asList(hobby));
	}
 }
Chinese scrambling resolution for doGet request:
// Get Request Parameters 
String username = req.getParameter("username");
//1 encode with iso8859-1//2 and decode with utf-8 
username = new String(username.getBytes("iso-8859-1"), "UTF-8");
Chinese scrambling resolution for POST requests:
@Override 
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
// Set the character set of the request body to UTF-8 to resolve Chinese scrambling in post requests 
	req.setCharacterEncoding("UTF-8"); 
	System.out.println("-------------doPost------------"); 
	// Get Request Parameters 
	String username = req.getParameter("username"); 
	String password = req.getParameter("password"); 
	String[] hobby = req.getParameterValues("hobby"); 
	System.out.println("User name:" + username); 
	System.out.println("Password:" + password); 
	System.out.println("Hobby:" + Arrays.asList(hobby)); 
}

Transfer of Request

ssdss What is request forwarding?

Ssdsds request forwarding refers to a request forwarding operation in which the server jumps from one resource to another after receiving a request.


ssdsdsdsServlet1 code:

public class Servlet1 extends HttpServlet { 
@Override 
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
	// Get the parameters of the request (materials for the transaction) View 
	String username = req.getParameter("username"); 
	System.out.println("stay Servlet1(View parameters (materials) in cabinet 1:" + username); 
	// Stamp the material and pass it to Servlet2 (Counter 2) to see 
	req.setAttribute("key1","CHAPTER 1"); 
	// Question: Where to go for Servlet2 (Counter 2) 
	/**
	* Request forwarding must start with a slash, /slash for address:http://ip:port/Project name/, web directory mapped to IDEA code <br/> 
	**/ 
	RequestDispatcher requestDispatcher = req.getRequestDispatcher("/servlet2"); 
	// RequestDispatcher requestDispatcher = req.getRequestDispatcher("http://www.baidu.com"); 
	// Go to Sevlet2 (Counter 2) 
	requestDispatcher.forward(req,resp);
	} 
}

ssdsdsdsServlet2 code:

public class Servlet2 extends HttpServlet { 
	@Override 
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
	// Get the parameters of the request (materials for the transaction) View 
	String username = req.getParameter("username"); 
	System.out.println("stay Servlet2(View parameters (material) in counter 2:" + username); 
	// Check if counter 1 has a seal 
	Object key1 = req.getAttribute("key1"); 
	System.out.println("Is there a chapter on counter 1:" + key1); 
	// Handle your own business 
	System.out.println("Servlet2 Handle your own business "); 
	} 
}

Role of base Tags

<!DOCTYPE html> 
<html lang="zh_CN"> 
<head> 
	<meta charset="UTF-8"> 
	<title>Title</title> <!--base Label sets the address the page refers to when working with relative paths href Property is the address value of the parameter --> 
	<base href="http://localhost:8080/07_servlet/a/b/"> 
</head> 
<body> 
	This is a Lower b Lower c.html page<br/> 
	<a href="../../index.html">Jump back to home page</a><br/> 
</body> 
</html>

Relative and absolute paths in the Web

In javaWeb, ssdss paths are divided into relative paths and absolute paths:

ssdss relative path:

Ssdsds.Represents the current directory ssdsds..Represents the parent directory SSDSS resource name Represents the current directory/resource name

ssdss absolute path:

ssdsdsdshttp://ip:port/Project/Resource Path

ssdsdsds Note: In actual development, paths use absolute paths rather than relative paths. 1, absolute paths 2, base+relative

Different meanings of slashes on the web

ssdss is an absolute path on the web / slash.

If the ssdss/slash is parsed by the browser, the address is:http://ip:port/

If ssdss/slash is resolved by the server, the address is:http://ip:port/Project Path

ssdsddss①,< url-pattern>/servlet1< /url-pattern>

ssdsddss②,servletContext.getRealPath("/");

ssdsddss③,request.getRequestDispatcher("/");

ssdss special case: response.sendRediect("/");Send the slash to the browser for parsing.http://ip:port/

HttpServletResponse class

Role of the HttpServletResponse class

The ssdssHttpServletResponse class is the same as the HttpServletRequest class. Each time a request comes in, the Tomcat server creates a Response object that is passed to the Servlet program for use. HttpServletRequest represents the requested information, HttpServletResponse represents the information for all responses, and we can set the information returned to the client throughHttpServletResponse object to set

Description of two output streams:


ssdss Note: Only one stream can be used for both streams. If byte streams are used, character streams cannot be used anymore and vice versa, otherwise errors will occur.

How to return (string) data to the client:

public class ResponseIOServlet extends HttpServlet { 
@Override 
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
		// Requirement: Return string data to client. 
		PrintWriter writer = resp.getWriter(); 
		writer.write("response's content!!!"); 
	} 
}

Response scrambling resolution:

ssdsdsds Solution Response Chinese Scrambling Scheme 1 (not recommended):

// Set the server character set to UTF-8 
resp.setCharacterEncoding("UTF-8"); 
// Set up the browser to also use UTF-8 character set via the response header 
resp.setHeader("Content-Type", "text/html; charset=UTF-8");

ssdsdsds Solution Response Chinese Scrambling Scheme 2 (Recommended):

// It sets the UTF-8 character set for both the server and the client, as well as the response header 
// This method must be called before obtaining the stream object to be valid 
resp.setContentType("text/html; charset=UTF-8");

Request redirection:

ssdss request redirection refers to when a client sends a request to a server and the server tells the client that. I'll give you some addresses. You go to a new address to access. Call for request redirection (because the previous address may have been discarded).


The first scenario for ssdss requesting redirection:

// Set response status code 302 to indicate redirection, (moved) 
resp.setStatus(302); 
// Set the response header to indicate where the new address is 
resp.setHeader("Location", "http://localhost:8080"); 

Second scenario for ssdss requesting redirection (recommended):

resp.sendRedirect("http://localhost:8080");

💖Thank you for your repeated attacks💖

Tags: Java Tomcat mvc java web

Posted on Sat, 18 Sep 2021 15:36:29 -0400 by msinternet