JSP realizes system login

The computer realizes resource access through the uniform resource locator, URL: Uriform Resource Locator

Directory structure of the Tomcat server

  • /bin: Stores script files for starting and stopping Tomcat under various platforms
  • /conf: store various configuration files of the Tomcat server
  • /lib: Stores various JAR files required by the Tomcat server
  • /logs: Stores Tomcat's log files
  • /temp: used to store temporary files when Tomcat is running
  • /webapps: When publishing a web application, the files of the web application are stored in this directory by default
  • /work: Tomcat puts the Servlet generated by JSP in this directory

Directory structure of a web application

  • /: The root directory of the web application, all files in this directory can be accessed by the client (JSP, HTML, etc.)
  • /WEB-INF: Stores various resources used by the application. This directory and its subdirectories are inaccessible to clients
  • /WEB-INF/classes: store all the class files in the web project
  • /Web-INF/lib: Stores JAR files used by web projects

During the running process of Tomcat, the class files in the classess directory are loaded first, and then the classes in the lib directory are loaded. If a class with the same name exists in the two directories, the class in the classes directory has priority.

JSP (Java Server Pages) are Java pages that run on the server side and are implemented using HTML nested Java code.

Its working principle can be summarized as follows: Java code is embedded in the JSP page, then the JSP page is compiled and executed to the server, interacts with the server, and then returns the page information to the client.

Three stages of web container processing of JSP requests

  • Translation phase: generating Java files
  • Compilation phase: generate class files
  • execution phase

When the page is requested for the second time, the web container will directly execute the bytecode file compiled last time without recompiling.

page directive in JSP

  • Define properties for the entire page by setting multiple properties inside
  • grammar:
<%@page attribute 1 = "attribute value" attribute 2 = "attribute value 1, attribute value 2" ... attribute value n = "attribute value n" %>
  • Example
%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

Common properties

  • language: specifies the scripting language used by the JSP page, the default value is java
  • import: Use this attribute to refer to the class file used by the scripting language, there is no default value
  • contentType: used to specify the encoding method used by the JSP page, the default value is text/html, ISO-8859-1

Small scripts and expressions in JSP

Small script syntax: <% code%>

Expression syntax: <%= code%

Small script example:

<%
    // The nested is java code 
   int sum=10+20 ;
    // Output to console 
   System.out.println("sum="+ sum);
    // Output to page out built-in object 
   out.println(" sum="+ sum);
  %>

Among them, System.out.println() is output to the console, and out.println() is output to the page

Expression example:

<%=sum %>

In this example, the value of sum is 30, and the value of the expression output is 30.

A scriptlet and an example of an expression that prints the current date:

 <%
    SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日");
    String time=format.format(new Date());
     %>
     <br/>
     <!-- Output current time -->
    <%=time %>

In the above example, the simpleDateFormat method formats the date so that the format is yyyy year MM month dd day.

Methods defined in JSP pages

grammar:

<%!Java code%>

The difference from the small script is an extra exclamation mark.

Small example: write a jsp page to calculate the sum of all even numbers between 1-100

<%
    int oushusum=0;
    for(int a=0;a<=100;a=a+2){
        oushusum = a + oushusum;
    }     
%><br>
    <%="Sum of 1-100 even numbers: "+oushusum %>

Make improvements to the "Output Current Time" page above, so that the code for formatting the time is encapsulated in a method. as follows:

 <%!
    public String formatDate(Date d){
    SimpleDateFormat fma=new SimpleDateFormat("yyyy-MM-dd");
    return fma.format(d);
    }
 %>
 <%=formatDate(new Date())%>

When the method has a return value, you can use the expression to output, when the method has no return value, you can use a small script to directly call. After the method is declared, it can be called in multiple places on the page.

Notes

The comments of HTML code in the page can be seen on the client side, while the JSP comments are not visible on the client side.

<!--The client can view it-->
<%--Client cannot view --%>

JSP built-in objects

JSP built-in objects are a group of objects created by the Web container, such as out objects, without the new keyword, but can be used.

<%
int[] value={56,65,45,65};
for(int i:value){
      out.println(i);      
    }
%>

Commonly used JSP built-in objects

out, request, response, session, application, pageContext (page context object), config (configuration object), page (page object), exception (exception object)

  • out object

The out object is a built-in object of JSP, which can be used without instantiation, and can realize the output display of data. Its methods are: print (display to the page output), println (display to the page output, add a newline at the end)

  • request object

The request object is mainly used to process client requests

Commonly used methods

  1. String getParameter(String name): Get the submission data according to the form component name
  2. String[] getParameterValues(String name): Get the request data when the form component corresponds to multiple values
  3. void setCharacterEncoding(String charset): specifies the encoding of each request
  4. RequestDispatchergetRequestDispatcher(String path): Returns a RequestDispatcher object whose forward() method is used to forward requests

Example:

Get the information submitted by the user in the user form (username, password, value of the check box), the methods used: String getParameter(String name), String[] getParameterValues(String name)

  // Get username 
    String username=request.getParameter("username" );
     // Get password 
    String pwd=request.getParameter("password" );
     // Get hobby 
    String[] hobbys=request.getParameterValues("hobby") ;

Garbled processing

No matter using the get method or the post method to pass the value, if the value is Chinese, the page will generate garbled characters. One of the ways to deal with garbled characters in the get method is to modify the configuration file of the tomcat server. The path is: Tomcat directory structure\conf\server.xml, append URLEncoding="UTF-8". See the code block below:

         <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8"/>

One of the ways to deal with garbled characters in the post method is to add the following code to the front of the java code that processes the form and specify it as UTF-8.

request.setCharacterEncoding("UTF-8");

Use properties to save data

grammar

request.setAttribute(String name, Object obj);

Among them, name is the property name, obj is the value of the property, example:

request.setAttribute("name", username);

read data from properties

grammar

request.getAttribute(String name);

Among them, name is the attribute name, example:

request.getAttribute("name");

When using properties to access data, make sure that the data must be of type Object when saved, and converted to its original type when retrieved.

  • response object

The response object is used to respond to client requests and output information to the client

Commonly used methods

  1. void sendRedirect(String localtion): Relocate the request to a different URL, i.e. page redirection

Example:

response.sendRedirect("login.jsp");

page forwarding

Forwarding can be used to carry information to other pages. In the case of login, page forwarding can be used to output relevant information on the login success page.

grammar

request.getRequestDispatcher(address of the page reached).forward(request, response);

Example

request.getRequestDispatcher("success.jsp").forward(request, response);

Comparison of forwarding and redirecting

Forwarding: The web server calls internal methods to implement request processing, and in the same web application, the request data is shared.

Redirection: After the web server returns a response, the browser sends a new request again, and the data cannot be shared after redirection.

Forwarding: The server side plays a role in passing data between multiple pages through the forward() method.

Redirection: It works on the client side and completes the page jump through a new request.

Forwarding: The transfer of internal control, the address bar does not change.

Redirects: New requests cause the address bar to change.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324823157&siteId=291194637