Java study notes-Day54 Servlet and JSP (two)



1. The role of Servlet and JSP


Servlet: background interface, provides data to the foreground program, and obtains data from the business layer. Servlet will not be used to generate dynamic pages, but will be used to receive requests from JSP, process the requests, and then go to the JSP page to display the results to the client.

JSP: Responsible for interacting with users, displaying data, and user input. The operation conforms to user habits and is beautiful. JSP is suitable for generating dynamic pages, because the static part of them can be directly used in HTML.

Second, the interaction between Servlet and JSP


(1) Pass data from Servlet to JSP

  • Servlet: ① Set data through the setAttribute method of the built-in object request (HttpServletRequest). ② Set the data through the setAttribute method of the built-in object session (HttpSession).

  • JSP: ① Get data through the getAttribute method of the built-in object request (HttpServletRequest). ② Get data through the getAttribute method of the built-in object session (HttpSession).

(2) Pass data from JSP to Servlet

  • JSP: ① Submit the form. ② url transfer value (similar to the get submission of the form).

  • Servlet: ① Get request parameters (single string) through the getParameter method of the built-in object request (HttpServletRequest). ② Get the request parameters (string array) through the getParameterValues ​​method of the built-in object request (HttpServletRequest).

note:

① If the obtained value is a null value, you can add an if statement in the code to determine whether the obtained value is null. Create a variable whose default value is an empty string. If the judgment value is not null, assign the obtained value to the change amount.

② If there are garbled Chinese characters, the request encoding format must be set to utf-8 (only valid for post) at the beginning of the program, and the form submission method of JSP must be set to post. The encoding format in JSP should also be set to utf-8.

	request.setCharacterEncoding("utf-8");
	
	String keywords="";
	if(request.getParameter("keywords")!=null) {
    
    
		keywords = request.getParameter("keywords");
	}

Three, the jump mode between Servlet and JSP

1. Forward


Forwarding: The speed is fast and the address bar remains unchanged. It is the internal behavior of the server and has nothing to do with the browser. The browser only obtains the data passed by the server. It can only be forwarded within the current project.

Request forwarding is also called direct forwarding (Forward), the client and browser only send a request once, Servlet, HTML, JSP or other information resources, the second information resource responds to the request, in the request object request, the saved object For each information resource is shared.

Realize the forwarding method one:

The RequestDispatcher interface defines the method of request forwarding:

Method declaration Method description
forward(ServletRequest request, ServletResponse response) Forward the request to other resources on the server, including other Servlet, JSP, etc.

To use the forward method, you need to obtain the RequestDispatcher object first. The method to obtain this object is provided in the request interface ServletRequest:

Method declaration Method description
RequestDispatcher getRequestDispatcher(java.lang.String path) Use path to return a RequestDispatcher object.
	// 从servlet跳转到jsp页面
	request.getRequestDispatcher("a.jsp").forward(request, response);

Realization of forwarding method two: jsp dynamic.

	<jsp:forward page="url地址"></jsp:forward>

2. Redirect


Redirection: The speed is fast, the address bar will change, each redirection request is related to the browser, and it can be redirected to any page (pages not in the current project can also be redirected).

Redirection is also called indirect forwarding (Redirect), which is actually two HTTP requests. When the server responds to the first request, the browser sends a request to another URL to achieve the purpose of forwarding.

Achieve redirection method one:

The redirection method is provided in the response interface HttpServletResponse:

Method declaration Method description
void sendRedirect(java.lang.String location) Redirecting to the location address is equivalent to the client re-requesting the resource where the location is located.
	response.sendRedirect("a.jsp");

Realization of redirection method 2: html a tag.

	<a href="url地址"></a>

Realization of redirection method 3: form get request (post is not redirection).

   <form action="ts.do" method="get"></form>

Realization of redirection method four: jstl label.

	<c:redirect url="ts.do"></c:redirect>

Implementation of redirection method five: javascript redirection.

	<script>
		location.href="ts.do";
	</script>

3. Jump between Servlet and JSP

3.1, Servlet jump to JSP


(1) Forwarding (only forwarded to the current project file, data can be carried)

	request.setAttribute("list", list);
	request.getRequestDispatcher("index.jsp").forward(request, response);

(2) Redirection (can be redirected to this project file or to the URL address of an external project)

	response.sendRedirect("../../../../admin/admin.jsp");

3.2, JSP jump to Servlet


(1) Forwarding (only forwarded to the current project file, data can be carried)

  1. method one:
	<% 
		request.setAttribute("key", "value");
		request.getRequestDispatcher("ts1.do").forward(request, response);
	%>
  1. Way two:
	<jsp:forward page="ts1.do"></jsp:forward>

(2) Redirection (can be redirected to this project file or to the URL address of an external project)

  1. method one:
	<%
		response.sendRedirect("ts1.do");
	%>
  1. Way two:
	<a href="ts1.do" >跳转到servlet</a>
  1. Way three:
	<form action="ts1.do" method="get"></form>
  1. Way four:
	<script>
		location.href="ts1.do?...";
	</script>
  1. Way five:
	<c:redirect url="ts.do"></c:redirect>

Fourth, the way the client accesses the server-side Servlet


There are three ways for the client to access the server-side Servlet, namely:

(1) Enter the URL directly from the address bar to access: directly use the URL to access, which is the GET method and call the doGet method.

(2) Click on the hyperlink in the webpage to visit: the hyperlink visit is in the GET mode, and the doGet method is called.

  • In html or JSP:
   <!-- 传递数据的方式: url传值 ?key=value&key=value key为参数名 -->
   <a href="a.do?name=tom&pwd=123456">提交请求到Servlet</a>
  • In the doGet method of Servlet:
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
		//获取url传递过来的值
		String name = request.getParameter("name");//变量name的值为tom
		String pwd = request.getParameter("pwd");//变量pwd的值为123456
		System.out.println("name :"+name+", pwd :"+pwd);
	}

(3) Access via form submission in the web page: Form submission access depends on the value of the method attribute of the form. The default is get. When the post is specified, the doPost method is called.

  • In html or JSP:
   <!-- 传递数据的方式: 表单传值 get方法 ?key=value&key=value  key为参数名 -->
   <form action="ts.do" method="get">
     <input type="text"  name="name" value="tom"><br/>
     <input type="password"  name="pwd" value="123456"><br/>
     <input type="submit" value="提交">
   </form>
  • In the doGet method of Servlet:
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
		//获取url传递过来的值
		String name = request.getParameter("name");//变量name的值为tom
		String pwd = request.getParameter("pwd");//变量pwd的值为123456
		System.out.println("name :"+name+", pwd :"+pwd);
	}

Guess you like

Origin blog.csdn.net/qq_42141141/article/details/111601708