JSP execution process

  • Jsp-java (Servlet file) -class
  • JSPS and servlets can be converted to each other because the first request server will have translation and compilation process, so it is slow; Subsequent accesses can be directly to class, which is faster. However, if the server modifies the code, it will be retranslated and compiled when accessed again.
  • Eclipse accidentally closes other page views, which you can do by using Windows->Perspective->Restart Perspective… To recover, but also in the



    Select the view to restore.
  • Tomcat change port number: server. XML in conf folder of Tomcat installation directory<Connector></Connector>Under the label change

1. Use Eclipse to develop Tomcat for Web projects (JSP projects)

2. Web projects created in Eclipse: the browser can directly access files in WebContent,

  • For example, http://localhost:8888/MyJspProject/index1.jsp the index1. JSP in the WebContent directory; But files in web-INF cannot be accessed directly by a client (browser), only by request forwarding
  • Note: Not any internal jump can access web-INF; The reason is that there are two types of redirect: request forwarding and redirection

3. Configure the Tomcat runtime environment JSP <->Servlet

  • A. Add servlet-api.jar in tomcat/lib to the project build path
  • B. Right-click project ->Build Path -> Add Library ->Server Runtime

4. To deploy tomcat

  • Create a new Tomcat instance in the Servers panel and run it after deploying the project in that instance (right-click -add)

  • Note: It is generally recommended to keep the configuration information of Tomcat in Eclipse consistent with that of local Tomcat: Set Tomcat in Eclipse to hosted mode: [first] After creating the Tomcat instance, double-click and select the second item in the Server Location

    5. Unified Character Set coding

  • A. Encoding classification: set JSP encoding (JSP file in the pageEncoding attribute) : JSP -> Java set browser read JSP file encoding (JSP file in the content attribute) generally set the above to a consistent encoding, utF-8 text encoding is recommended:

    • I. Uniform setting of files throughout Eclipse (recommended)

    • Ii. Set a project

    • Iii. Set up separate files

    6.JSP page elements: HTML Java code (script Scriptlet), instructions, comments

  • A.

<% Local variables, Java statements %>Copy the code
ii.
Copy the code
The < %! Global variables, definition methods %>Copy the code
iii.
Copy the code
<%= Output expression %>Copy the code
  • Generally speaking, you need to restart the Tomcat service to modify web. XML, configuration files, Java, but you do not need to restart the Tomcat service to modify Jsp\ HTML \ CSS \js
  • Note that out.println() cannot enter; To enter:”<br/ >“, i.e. out.print() <%= %> can parse the HTML code directly
  • B. Command Page command

The < % @ page… PageEncoding: JSP file encoding JSP -> Java contentType: JSP encoding parsed by browser

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"  import="java.util.Date"% >Copy the code

C. Comments HTML comments, can be viewed by the client through the browser to see the source code Java comments // /… / JSP comments <%– –%>

  • 7.JSP nine built-in objects (built-in, do not need to use the new object) out of the client output content request object; Common ways to store a request object for information sent from a client to a server: String getParameter(String name) : Based on the requested field name key (input tag name attribute value), Value (value of the input tag) String[] getParameterValues(String name): Value (checkbox) void setCharacterEncoding(” UTF-8 “) : Forward (request,response) getRequestDispatcher(” b.jsp “). Forward (request,response); A – > B ServletContext getServerContext(): Gets the ServletContext object for the project

Example:

Note: JSP does not need to restart Tomcat for each test, just save the project to level 2 and refresh

You need to restart Tomcat to add the pages initially visited by the project under web.xml, such as register.jsp to



When it starts, it will automatically search for it according to the following page

registered

register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"% > <! DOCTYPE html> <html> <head> <meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="show.jsp" method="post"> User name: <input type="text"  name="uname"/><br/> Password: <input type="password"  name="upwd"/><br/> Age: <input type="text"  name="uage"/><br/> <input type="checkbox"  name="uhobbies" value="Football"/> <input type="checkbox"  name="uhobbies"  value="Basketball"/> <input type="checkbox"  name="uhobbies"  value="Table tennis"/> table tennis <br/> <input type="submit" value="Registered">
		</form>
</body>
</html>
Copy the code

show.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"% > <! DOCTYPE html> <html> <head> <meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		// Set the post request encoding
		request.setCharacterEncoding("utf-8");
	
		String name = request.getParameter("uname");
        //name = new String( name.getBytes("gbk") , "utf-8"); Get modifies the code
        
		// Convert a string to an integer
		int age = Integer.parseInt(request.getParameter("uage"));
		String pwd = request.getParameter("upwd");
		
		String[] hobbies = request.getParameterValues("uhobbies"); % > registration, information is as follows: < br > name: < % = name % > < br > age: < % = age % > < br > password: < % = PWD % > < br > interest: < br > < %if(hobbies ! =null) {for(String hobby :hobbies)
		{
			out.print(hobby+"&nbsp;");
		}
		
		}
	%>
</body>
</html>
Copy the code

http://localhost:8888/MyJspProject/show.jsp?uname=aa&upwd=123&uage=22&uhobbies=%E7%AF%AE%E7%90%83 connection/file? Parameter name 1= Parameter value 1 & Parameter name 2= Parameter value 2 & Parameter name 1= Parameter value 1

  • Get submission mode: method= Get, address bar, and hyperlink () request modes are all get submission modes by default
  • The differences between GET and POST are as follows: a. Get displays the request information in the address bar (the address bar can contain only 4-5KB of information). If the requested data exists in a large file, images, etc., will appear the address bar cannot accommodate all the data and error); Post is not displayed b. Upload operations must be POST. Post is recommended

If garbled characters occur in a request, solve the following problem:

  • A. Uniform coding of each variable (not recommended)

    New String(old encoding, new encoding);



    Name = new String (the name getBytes (” iso – 8859-1 “), “utf-8”);
  • B. Modify server. XML to change the encoding of the default TOMCAT GET submission mode (UTF-8). You are advised to use tomcat to synchronize the encoding of the GET submission mode in server. XML. URIEncoding = “utf-8”
 <Connector port="8080" protocol="HTTP / 1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8/>
Copy the code

Tomcat7 (default ISO-8859-1)

Tomcat8 (default UTF-8)

Set up a post request code request. SetCharacterEncoding (” utf-8 “);