JSP basics and jsp Basics

Source: Internet
Author: User

JSP basics and jsp Basics

JSP is the abbreviation of Java Server Pages, that is, the Server-side Java page.

There are three methods to annotate JSP:

1. HTML comments, <! -- Comment content -->. This annotation client is visible and has security risks.

2. JSP comment, <% -- Comment content -->

3. Java Script annotation. Three Java annotations are used in JSP scripts.

Command labels are used to define information about the entire JSP page.

Syntax:

<%@ directive attribute="value" %>

Directive command name. JSP contains three commands: page, include, and taglib.

Attribute, which is the attribute of the command.

Value, which is the value of the corresponding attribute of the command.

Note: <% @ %> is a complete mark, and spaces cannot be added in the middle.

For example, the page command defines the global attributes of a JSP file through multiple internal attributes,

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

In JSP, <% out. print (); %> is used for output. The disadvantage of this output is also obvious, so that the Java code and HTML Tag are mixed, with poor readability and not conducive to modification.

For example:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

To simplify the output, we can use <% = Java expression %>.

The Java expression here is usually a Java variable or a method with a return value.

For example:

<% String title = "news title" %> <% = title %>

Note: variables or methods cannot end with semicolons. Otherwise, an error is reported.

We also need to escape alignment when outputting some special characters.

For example: <%, %> ,',",\.

These characters have special meanings and purposes in JSP, so escape these characters in JSP programs.

Start tag <%, changed to <%.

End tag %>, changed to % & gt.

Add the Escape Character \ before using other characters.

For example:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

If some Java classes are used in JSP, you must use <% @ page import = "package name" %> to import the package at the beginning of the file.

For example:

<%@ page import="java.util.*"%>

JSP Execution Process: ** _ jsp. java --> compile ** _ jsp. class --> Execute

In JSP, you can also declare global variables by using the following methods:

<%! Global variable %>

You only need to call the variable name and the method at the same time.

The most common errors in web programming:

404 error ---- the accessed page or resource cannot be found. Cause of error: the URL entered during running is incorrect, the page is placed in the web-inf directory, Tomcat is started externally, and no project is deployed.

500 Error ---- JSP page code error. Error cause: JSP code error.


Jsp entry-level questions

I wrote four jsp files.
1. user. jsp
<% @ Page language = "java" import = "java. util. *" pageEncoding = "GBK" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
<Html>
<Body>
<Form name = "form" action = "do. jsp">
Enter the User name: <input type = "text" name = "username" value = "">
<Input type = submit name = "submit" value = "submit">
</Form>
</Body>
</Html>

2. do. jsp
<% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
<Html>
<Body>
<%
String user = request. getParameter ("username ");
%>
<%
If (user. equals (""))
{%>
<Jsp: forward page = "/false. jsp"> </jsp: forward>
<%} Else {%>
<Jsp: forward page = "/success. jsp">
<Jsp: param name = "username" value = "<% = user %>"/>
</Jsp: forward>
<% }%>
</Body

3. false. jsp

<% @ Page language = "java &... the remaining full text>

Introduction to JSP ......

The concept is not explained by others. Set the following differences
After reading a classic article, JSP writes JAVA code in HTML, while SERVLET writes HTML in JAVA.
JSP focuses on page display, while SERVLET focuses on business and logic.
JSP is a derivative of SERVLET (it is difficult for SERVLET to control the display of pages, leading to the appearance of JSP)
During JSP execution, the SERVLET file will be converted before being interpreted by the server.
TOMCAT is an open-source server. to access your page, you must first apply to the local server through the network, and then the server will convert the local content into HTML and pass it to the visitor.
Just like online shopping, goods are in another person's place. You must first apply to buy something from him. After receiving your shopping request, he will package it and send it to you. The server is required!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.