JavaWeb -- build Web environment | JSP output

Article directory

I. URL

A computer accesses resources through a URL (Uniform Resource Locator). A URL is the only naming convention that can identify a specific computer, directory or folder location on the Internet.

2, B/S program architecture

B/S (Browser/Server) structure is a kind of network structure mode which rises with the development of Internet and the emergence of Web. This mode unifies the client side and enables the core business processing to be completed on the server side. You only need to install a browser on your own computer or mobile phone to interact with the database through the Web Server.

B/S architecture uses request / response mode for interaction.

3, Web server

3.1 definition

A Web server is a program that can provide documents to the requesting browser as well as information browsing service on the Internet.

3.2 common Web servers

  • Microsoft: IIS
  • Apache: Tomcat
  • Oracle: WebLogic
  • IBM: WebSphere
  • Nginx

3.3 Tomcat server

3.3.1 definition

Tomcat server is a Servlet container developed by the Jakarta project of Apache Software Foundation. It supports Servlet and JavaServer Page (JSP), and provides some special functions as Web server, such as Tomcat management and control platform, security domain management and Tomcat valve. Because Tomcat itself also contains an HTTP server, it can also be regarded as a separate Web server. However, Tomcat and Apache HTTP server cannot be confused. Apache HTTP server is an HTTP web server implemented in C language; these two HTTP Web servers are not bundled together. Apache Tomcat includes a configuration management tool, which can also be configured by editing the configuration file in XML format.

3.3.2 characteristics

  • Apache Jakarta's open source project
  • Lightweight application server
  • Open source, stable and small resource occupation

3.3.3 directory structure after installation

Download address: http://tomcat.apache.org/

3.3.4 configuration

Default port number: 8080.
Modify the Tomcat port number through the configuration file server.xml.

3.3.5 boot

  • bin\startup.bat
  • Visit http://localhost:8080 to view the Tomcat home page

3.3.6 stop Tomcat

  • bin\shutdown.bat

Four, JSP

4.1 definition

JSP (JavaServer Pages) is a kind of technical standard which is advocated by Sun Microsystems and participated by many companies to enable software developers to respond to client requests and dynamically generate HTML, XML or other format documents. JSP technology is based on Java language as script language. JSP Web page provides an interface for the whole Java library unit on the server side to serve HTTP applications. JSP is a Java page running on the server side, which is implemented by HTML nesting java code

The Java server page (JSP) is an extension of HttpServlet. Because most of httpservlets are used to respond to HTTP requests and return Web pages (such as HTML and XML), it is inevitable that a large number of HTML content will be involved in writing servlets, which brings great obstacles to the writing efficiency and readability of servlets. JSP is generated on this basis. Its function is to use HTML writing format, add Java code snippets in appropriate places, free programmers from complex HTML, and focus on the content of servlet itself.

JSP is converted into servlet by application server when it is accessed for the first time. In the later run, the container calls the servlet directly instead of visiting JSP page. The essence of JSP is still servlet.

4.2 working principle

4.3 page instruction

The properties of JSP are defined by attributes to realize the communication with JSP container.

Syntax:

<%@ page language="Attribute value" import="Attribute value" contentType="Attribute value"%>

Properties:

  • Language: Specifies the scripting language used by the JSP page
  • import: use this property to reference the class file used in the scripting language
  • contentType: used to specify the MIME type of the page and the character encoding method

Example:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.Arrays" %>
<%@ page import="java.util.List" %>

4.4 output using JSP

There are detailed comments in the code!

4.4.1 example 1

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.Arrays" %>
<%@ page import="java.util.List" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp</title>
	<!-- Use<%%>Declaring local variables, using<%!%>Declare global variables -->
	<%
		int age=18;
		String name="Jack";
		int[] arr = new int[]{1,2,3,4};
	%>
</head>
<body>
	<h1>This is my first jsp Page!</h1>
	<h2><a href="/day_0903_web1/index2.jsp">Demonstration Java Code and html Mixed use of labels</a></h2>
	<%
		// Java comments are not visible
		out.println(name+"<br />");	// Line break after ln output
		out.println(age);
	%>
	
	<!-- Java Another format for outputting content to a page -->
	<!-- Output content to page only, not even semicolon -->
	
	<%=name %>
	
	<!-- Output the contents of the array to the page -->
	<!-- 1 Using cycle output -->
	<%
		for(int i=0; i<arr.length; i++){
			out.print(arr[i]+" ");
		}
	%>
	<!-- 2 Arrays -->
	<%=Arrays.toString(arr) %>
</body>
</html>

Effect:

4.4.2 example 2\

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.Map.*" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>java and html Mixed writing exercise</title>
<%
	List names = new ArrayList();
	names.add("Jack");
	names.add("Rose");
	names.add("Tom");
	List ages = new ArrayList();
	ages.add(18);
	ages.add(19);
	ages.add(20);
%>
</head>
<body>
	<!-- Output a table to the page -->
	<!-- Java Output in code html Label -->
	<%
		out.print("<table>");
		out.print("<tr><td>number</td><td>Full name</td><td>Age</td></tr>");
		for(int i=0; i<names.size(); i++){
			out.print("<tr><td>"+i+"</td><td>"+names.get(i)+"</td><td>"+ages.get(i)+"</td></tr>");
		}
		out.print("</table>");
	%>
	
	<!-- Above is the use of Java Output html Label -->
	<!-- In general, it is recommended that Java Code and html Label separation -->
	<hr />
	<table>
		<tr><td>number</td><td>Full name</td><td>Age</td></tr>
		<%
		for(int i=0; i<names.size(); i++){
		%>
			<tr><td><%=i %></td><td><%=names.get(i) %></td><td><%=ages.get(i) %></td></tr>
		<%} %>
	</table>
	
	<hr />
	<%
	Map<Integer, String> cts = new HashMap<Integer, String>();
	cts.put(1,"Toys");
	cts.put(2,"Stationery");
	cts.put(3,"book");
	%>
	
	<!-- Output to select Label -->
	<%
	Set<Entry<Integer, String>> e = cts.entrySet();
	Iterator<Entry<Integer, String>> it = e.iterator();
	%>
	
	<select name="ct_id">
	<%
	while(it.hasNext()){
		Entry<Integer, String> entry = it.next();
	%>
	<option value="<%=entry.getKey() %>"><%=entry.getValue() %></option>
	<%
	}
	%>
	</select>
	
	<hr />
	<!-- Output to radio Label -->
	<%
	Set<Integer> ketSet = cts.keySet();
	for(Integer key:ketSet){
	%>
	<input type="radio" name="ctl" value="<%=key %>" /><%=cts.get(key) %> &nbsp;&nbsp;
	<%
	}
	%>
</body>
</html>

Effect:

Published 12 original articles, won praise 11, visited 380
Private letter follow

Tags: Java JSP Tomcat Web Server

Posted on Wed, 05 Feb 2020 02:56:09 -0800 by jami3