${pagecontext.request.contextpath} in Java

Source: Internet
Author: User

Before in the DRP project contacted ${pagecontext.request.contextpath}, then did not notice, this time in the Java version of the University cloud Platform ITOO4.0 meet again, really wuqiaobuchengshu ah. Once again, I will ignore it, it is not justified.

Before we say this, we have to talk about the El expression in a nutshell.

  el expression

EL full name is Expressionlanguage. Functions are:

? Get Data: 

El expressions are primarily used to replace script expressions in JSP pages to retrieve Java objects from various types of Web domains and get data. (objects in a web domain, accessing JavaBean properties, accessing the list collection, accessing the map collection, accessing an array)

? Perform the operation: 

The EL expression allows you to perform some basic relational, logical, and arithmetic operations on a JSP page to accomplish some simple logic operations in a JSP page. ${user==null}

? Get web Development Common objects 

EL expressions define implicit objects that make it easy for web developers to get a reference to a common Web object to get the data in those objects.

? Calling the Java method 

The EL expression allows the user to develop a custom El function that invokes a method of the Java class through an El expression in a JSP page.

  The following are the main ways to get data:

Get data syntax using el expression: "${ identifier }"

EL when an expression statement executes, it invokes the Pagecontext.findattribute method, using the identifier as the keyword, respectively, from the page , Request , Session , Application Find the appropriate object in four fields, return the object if found, and return "" (Note that it is not NULL , but an empty string).

  ${pagecontext.request.contextpath}

{PageContext.request.contextPath} is actually a type of El expression that takes the data from the Get method using the principle of the El Get value:

pagecontext.getrequest () get the HttpServletRequest object, this is the first step, the second step is to use the HttpServletRequest to call the Getcontextpath method.

The role is to remove the deployed application name so that no matter how it is deployed, the path used is correct.

The pros and cons of other ways to get paths:

Http://www.cnblogs.com/langtianya/archive/2013/03/04/2942515.html

In addition, everyone used to use JSP script output: The specific is:

 <%=request.getcontextpath ()%>, the function is to return the name of the app where the current page is located.

? El better than script <%%>  

1. El expression is represented by ${}, which can be used in all HTML and JSP tags instead of the complex Java code in JSP pages.

2. An El expression can manipulate constant variables and implicit objects. The most commonly used implicit objects are ${param} and ${paramvalues}. ${param} represents the return value of a single string in the request parameter. ${paramvalues} Represents a set of values that return a request parameter. pagescope represents a variable for a page range. Requestscope represents a variable for the Request object. Sessionscope represents a variable in the session scope. Applicationscope represents the variable that is scoped.

3. <% @pageisELIgnored = "true"%> indicates whether the El language is disabled, and true to suppress. False indicates that the. JSP2.0 enabled El language is not suppressed by default.

4. The El language can display a logical expression such as ${trueand false} The result is a false relationship expression such as ${5>6} result is false arithmetic expression such as ${5+5} result is 10

5. The variable search scope in El is: page,equest,session,application dot operator (.) and "[]" are the values that represent getting variables. The difference is [] A variable that can display non-speech

 small expansion of knowledge

Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; This is also a very common expression in the DRP project, Commonly used before we use ${pagecontext.request.contextpath}.

So what exactly does that mean?

Seemingly incomprehensible, is actually to get the application root URL, or access address, for example, we often visit the local address of the 8080 port number, that is, http://localhost:8080;

Specific explanations:

1, Request.getscheme () returns the name of the protocol HTTP, and the following "://" to spell it becomes HTTP//

2, Request.getservername () This is the name of your server this machine is localhost or 127.0.0.1, which means the local

3, Request.getserverport () returns the port number, such as 8080

Summary

Before the three a little to remember, summed up a bit, suddenly more clear, knowledge or particles ah.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

${pagecontext.request.contextpath} in Java

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.