How to embed Java code in JSP and instructions

Source: Internet
Author: User

Three ways to embed Java code in a JSP:

(1) Declaring a variable or method: <%! Statement %>: use cautiously , because this method defines a global variable

(2) Java Fragment (scriptlet): <% java code; %>

(3) Expression: Used to output the value of an expression to the browser, <%= expression%> in the expression cannot be semicolon (;)

Instructions for JSP pages

<%@ page%>//This is generally written on the top of such as import and so on

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8" contenttype= "text/html; Charset=utf-8 "%>

directive: Set the properties and characteristics of a JSP page

Common directives:

(1) <%@ page%> or <jsp:directive.page >: Common Properties: Language= "" Contenttype= "Text/html;charset=utf-8"

pageencoding= "" Import= "Com.inspur.Dpet"

Iserrorpage= "True|false" Specifies whether the current page can be used as an error page

Errorpage= "error.jsp" (iserrorpage= "true" in error.jsp page)

(2) <%@ include file= "url"%> or <jsp:directive.include >

"Note" Static inclusions, which are included in the compile phase to compile a bytecode file together with other files

Question: (1). Included files cannot contain

(2). The value of the file property cannot be evaluated with an expression, not with an argument

Use of form, reset, and submit tags in HTML:

<form name= "LoginForm" method= "Post/get" action= "..." onsubmit= "return function ()" >

The action content is the URL in the servlet mapping

<input type= "reset" id= "reset" name= "reset" value= "reset"/>

<input type= "Submit" id= "submit" name= "submit" value= "Login"/>

</form>

The difference between post and get in the method of form form:

Post-submitted data is more covert and suitable for submitting large volumes of data

Http://localhost:8080/booklib/user

The data submitted by get is not well hidden, it will be displayed in the Address bar and the address bar can be up to 255 characters

http://localhost:8080/booklib/user?username=admin&password=123&submit= Login

The above method of transferring data via get can also be implemented via hyperlinks:

<a href= "user?username=admin&password=123&submit= Landing" ></a>

The effect is the same as the Get method, and the data submitted with the hyperlink can also be used with string userrname = Request.getparameter ("username"); Gets the data, where the parameter of the method is the data name before the equals sign, and the value is after the equal sign.

can also be: location.href= "user?action=del&uid=" +UID;

Or: Location.replace ("" User?action=del&uid= "+uid");

Location.href is equivalent to location.assign. (How to use????????? )

When using replace, the requested address will not be placed in the list????? What do you mean??

The value of the action in the form should be the value of Url-pattern in Web. xml:

The Web container parses this value after it receives this value, and then takes this value to the Url-pattern in Web. Xml to match until it finds the corresponding servlet class, then generates the servlet instance through the reflection mechanism and then to the service () in the servlet method, and then calls the corresponding Dopost () and Doget () methods according to whether a post or get is requested by method.

How to invoke the request and response objects in the JSP:

Request and Response object source: From Doget (httpservletrequest request, httpservletresponse response)

In the JSP can be used directly, the use of the following:

There are two common methods of request:

Request.setattibute ("key", value)/requeset.getattribute ("key")

Common use of setattribute () in Servlets

Generally used in JSP getattribute ()

Getattribuet () Gets an object type, which requires a forced type conversion

Request.setcharacterencoding ("UTF-8");

String username = request.getparameter ("username");

String Password = request.getparameter ("password");

Session Object Source: HttpSession session declared in doget () = Request.getsession (true);

Session:sessioin.setAttribute ("key", value);

Common use of setattribute () in Servlets

(Object) Session.getattribute ("key");

Generally used in JSP getattribute ()

RequestDispatcher objects in the servlet:

RequestDispatcher rd = Request.getrequestdispatcher (target);

Rd.forward (Request,response)

How to embed Java code in JSP and instructions

Related Article

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.