Java web (2) Basic grammar of JSP & form submission method

 

Table of contents

1. What is JSP?

2. Introduction to cs and bs

3. The three components of url

4. How to modify the default port number of Tomcat

5. Why do we need dynamic web pages based on bs architecture

6. Output the current date in the web page

7. The three stages that the web container needs to go through when processing JSP file requests

8.jsp page elements

9. page command

10. Three annotation methods in the jsp file:

11. Common errors when running the web

12. JSP built-in objects

13. Registration interface


1. What is JSP?

JSP is the file format of java+HTML+CSSjs

JSP is essentially a java file

Tomcat will turn jsp files into Java files

jvm (Java Virtual Machine) will turn Java into a class file for compilation

2. Introduction to cs and bs

CS is between client (client) and server (server). Its main features are strong interactivity, safe access mode, low network traffic, fast response, and good for processing large amounts of data. However, the program of this structure is developed in a targeted manner, and the changes are not flexible enough to maintain and management is difficult. Moreover, each client of this structure needs to install the corresponding client program, the distribution function is weak and the compatibility is poor, and it cannot be quickly deployed, installed and configured, so it lacks versatility and has great limitations.

Between bs browser (browser) and server (server), only one server (server) needs to be installed and maintained. Compared with cs, its main features are strong distribution, convenient and simple maintenance, easy development and strong sharing, but the data Security issues, high requirements on the server, and slow data transmission speed are obvious to all. For example: It is difficult to input a large amount of data through a browser or respond to a report and print out a special purpose. It is more difficult to implement complex application structures.

Common cs architecture: QQ, WeChat

Common bs architecture: Baidu

Of course, there are also some with both cs and bs architectures, such as the common b station

3. The three components of url

http(protocol)://localhost(host):8080(port)

4. How to modify the default port number of Tomcat

Double click on your server

 

 Click to modify

 How to solve if a port conflict is found?

Modify the port or restart

5. Why do we need dynamic web pages based on bs architecture

Before we run the project

First double-click our Tomcat server

Note: If the current server has a project running, the project must be removed before it can be selected

 Make the following selections:

After saving, when we run our project

The project we wrote will be moved to webapps by Tomcat for deployment

Then you will find that webo2 will appear in the webapps release directory, which is our new project

 

 At the same time, there will also be a web02 in our work compilation directory

 will eventually become a Java file

If you want to run it will become a class file

 

In the compiler we wrote

 

Open our Java file

 All code written on the web page will be converted to Java code by out.write()

 This is a conversion process, which means that the jsp files you write in jsp will eventually become Java files

6. Output the current date in the web page

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>web02</title>
</head>
<body>
	<%
			out.print(new Date());
	%>
</body>
</html>

The effect is as follows:

 7. The three stages that the web container needs to go through when processing JSP file requests

 Among them, each of your requests will only be compiled for the first time, and when you request for the second time, you will find that it has been compiled. It is not difficult to find that many web pages take a long time to enter for the first time, but the second time they are compiled It will be much faster than before, the reason is that it has been compiled, that is the reason. If the person is deleted, it needs to be recompiled.

Note: If the JSP file is modified, the Web container will re-translate and compile the JSP file

8.jsp page elements

 9. page command

  • Syntax: <%@ page attribute 1="attribute value" attribute 2="attribute value 1, attribute value 2"...attribute n="attribute value n"%>
  • Define properties for the entire page by setting multiple properties inside

      Attributes

describe

Defaults

language

Specifies the scripting language used by JSP pages

java

import

Use this attribute to refer to the class files used in the scripting language

none

contentType

Used to specify the encoding method used by JSP pages

text/html, 

ISO-8859-1

10. Three annotation methods in the jsp file:

  •  HTML comments: <!-- html comments --> Disadvantages: insecure, increase the burden of network transmission
  •  JSP comment: <%-- JSP comment--%>
  •  Comment in the JSP script: <% //single-line comment%> <% /*multi-line comment*/ %>

11. Common errors when running the web

  • The Tomcat service is not started, or the Tomcat service is not started in the expected port -> check whether Tomcat is running normally
  • Try to run the web program without deploying the web application -> check whether the web application is deployed normally
  • When running, the URL is entered incorrectly -> check the URL path
  • The directory where the file is stored cannot be referenced externally, such as: the file is placed in a folder such as WEB-INF, META-INF -> check the storage location of the file

12. JSP built-in objects

  • JSP built-in objects are a set of objects created by the web container
  • The names of JSP built-in objects are JSP reserved words
  • JSP built-in objects are objects that can be used directly in JSP pages without using "new" to obtain instances

For example:

 Can be used without new.

Common built-in objects

 common methods of request

          method name

            illustrate

String getParameter(String name)

Get page submission data according to page form component name

String[ ] getParameterValues(String name)

Get the user's request data when a page form component corresponds to multiple values 

void setCharacterEncoding(String charset)

Specify the encoding of each request and set it before calling getParameter( ), which can solve the problem of Chinese garbled characters

String getRemoterAddr()

Returns the IP address of the client that submitted the request

 code display

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<h1>这是处理登录请求的界面</h1>
<%
//内置用户: sa  123

//获取到用户提交的表单信息
String username = request.getParameter("username");//请求中获取数据
String password = request.getParameter("password");//请求中获取数据

//从请求中获取数据(类型全部是string,必须自己做转换)
String age = request.getParameter("age");
out.print(username + "-" + password + "-" + age);

String ts[] = request.getParameterValues("habit");//接收到多个具备相同name的值
//遍历
String my_habit = "";
for (String habit : ts) {
	System.out.print(habit);//显示在控制台
	my_habit += habit + ",";//字符串拼接
}

//存在数据库中应该为一个数据
out.print(my_habit);
my_habit.split(",");

//用于做登录验证
if ("sa".equals(username) && "123".equals(password)) {
	out.print("登录成功");
} else {
	out.print("登录失败");
}
%>



13. Registration interface

<form name="form1" method="post" action="reginfo.jsp">
    <table border="0" align="center">
      <tr> <td>用户名</td><td> <input type="text" name="name"></td></tr>
      <tr><td>密码</td><td > <input type="password" name="pwd"> </td></tr>
      <tr> <td>hello world</td><td> 
          <input type="checkbox" name="channel" value="报刊">报刊 
          <input type="checkbox" name="channel" value="网络">网络 
          <input type="checkbox" name="channel" value="推荐"> 推荐 
          <input type="checkbox" name="channel" value="电视"> 电视
        </td></tr>
       <!-- 以下是提交、取消按钮 -->
       <button>提交</button>
    </table>
</form>

Information reading interface

<%
	//读取用户名和密码
	String name = request.getParameter("name");
	String pwd = request.getParameter("pwd");
	//读取复选框选择项
	String[] channels = request.getParameterValues("channel");
	if (channels != null) {
	         for (int i = 0; i < channels.length; i++) {
	out.println(channels[i]);
	          }
	}
%>

14. How to solve the problem when the Chinese output is garbled?

During page development, there are multiple character set encoding settings, we only need to set the character set that supports Chinese characters on the page. like:

request.setCharacterEncoding("UTF-8");	

Today's sharing is here!

Guess you like

Origin blog.csdn.net/m0_67376124/article/details/123768013