[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 29091
  • Last Modified:

jsp getContextPath() and getRealPath()

Hi, jsp gurus,

Can anyone tell what's the difference between request. getContextPath()  and application.getRealPath("/")

i tried to print out : contextPath=<%=request.getContextPath() %>,  but it didn't work.

Thanks for your attention.
0
lilyyan
Asked:
lilyyan
  • 6
  • 6
1 Solution
 
bloodredsunCommented:
From javadocs

HttpServletRequest.getContextPath()
          Returns the portion of the request URI that indicates the context of the request.

ServletContext.getRealPath(java.lang.String path)

    Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext.

    The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).

-----------
Not to be confused with
getServletContext().getRealPath("/") ;
which returns the path to the current webapp are you?

0
 
lilyyanAuthor Commented:
Thanks for your reply. Would you please give an example, cause I'm still confused.
0
 
bloodredsunCommented:
Okay,

I have a webapp on my local machine called "scratchpad" where I do my development work. The url for the home page is
"http://localhost:8080/scratchpad/index.jsp".

If I have this code in my homepage
-----------------
HttpServletRequest.getContextPath(): <%=request.getContextPath()%><br>
ServletContext.getRealPath: <%=getServletContext().getRealPath("/")%>
----------------

it outputs
---------------
HttpServletRequest.getContextPath(): /scratchpad
ServletContext.getRealPath: C:\programs\tomcat4_1\webapps\scratchpad\


This is because the first outputs the name of the context (the webapp name). The second shows me the real path (the directory structure on the computer) of the webapp, i.e. where it can be found on my PC. Note that if you are developing in the root context (at url "http://localhost:8080/index.jsp") your request.getContextPath() will retrurn empty as their is no webapp name in the URL.

Hope that explains everything.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
lilyyanAuthor Commented:
Hi, thanks for your reply.

I'm using tomcat-5.5.7. My application is under : c:\jakarta-tomcat-5.5.7\webapps\ROOT\fileLists\

When I use contextPath=<%=request.getContextPath() %>, it return nothing.

1.fileLists is the web application name (jsut a directory) , so it should return /fileLists  ?

2. in tomcat, besides under the root directory, where the program could be put?


another question:
<%=getServletContext().getRealPath("/")%> and
<%=application.getRealPath("/") %>
3.return same thing, could explain a little bit the reason?

4. also should i always use the "/" as a premeter in getRealPath("/") ?

Thanks for your reply.
0
 
bloodredsunCommented:
>>1.fileLists is the web application name (jsut a directory) , so it should return /fileLists  ?
Nope, it should return nothing as your are in the ROOT context.

>.2. in tomcat, besides under the root directory, where the program could be put?
Create a new webapp. all this means is creating a new folder in <tomcat_home>/webapps. Here's a walk through

create folder <tomcat_home>/webapps/test
go to http://localhost:8080/manager/html (the tomcat admin, default user is "admin" , pwd is blank)
Scroll down to:
>Install
>Install directory or WAR file located on server
in >WAR or Directory URL:>
type: "test" (with no quote marks) and click the install button.
There should now be a webapp called "test" in the list at the top.
Remember, unless you have a web.xml in your test/WEB-INF folder, if you restart tomcat, you will lose the "test" webapp.

3./They refer to the same object. "application" is an implicit object in a jsp like request and response. Just as request is the HttpServletRequest object for that page, application is the ServletContext object for the page and you are calling the same object with both.

4./To get the root of that webapp, yes.

0
 
lilyyanAuthor Commented:
Hi, thanks for your reply. Very clear.

When I try to go to http://localhost:8080/manager/html 

I input the "admin" as user name ( no quotes ), click ok (pwd is blank).

The window is still there, it seems the pwd is not correct . Where i can find the admin. user name and pwd information?

Thanks for your reply.


0
 
bloodredsunCommented:
I don't know but you would have set this up when you installed tomcat. If you can;t remember then you might need to re-install tomcat as this is quite an importatnt thing.
0
 
lilyyanAuthor Commented:

now , i can access the manager page. when i scroll down to ( not install on the page, i'm using tomcat5.5.7))
>Deploy
>Deploy directory or WAR file located on server

there are several itemS need to be filled out
------------------------
Context Path (optional):    
XML Configuration file URL:    
WAR or Directory URL
---------------------------------
i typed "test" (no quotes) in WAR or Directory URL , and click the deploy button. and i got a error message:

FAIL - Invalid context path  was specified

since the context path is optional, i leave it blank

also there is an item
>WAR file to deploy
>Select WAR file to upload

do i need to fill this out ?

thanks for your reply.



0
 
bloodredsunCommented:
You need to create a folder in <tomcat_home>/webapps called test so that you have a folder called <tomcat_home>/webapps/test and then try to load the directory otherwise you're trying to load somehting that doesn't exist yet.
0
 
lilyyanAuthor Commented:
underC: \jakarta-tomcat-5.5.7\webapps, i create a fold called test. then type "test" in WAR or Directory URL , still got the same error. do i need to fill out the XML Configuration file URL:  ?

many thanks
0
 
bloodredsunCommented:
okay create one called blodoredsun and do the same thing. The only box you need to fill out is the one called "WAR or Directory URL:" , leave all the others blank.
0
 
lilyyanAuthor Commented:
maybe i'm using a different version of tomcat from yours.

>Deploy
>Deploy directory or WAR file located on server
------------------------
Context Path (optional):    
XML Configuration file URL:    
WAR or Directory URL
---------------------------------

in the second box, it's not optional.

if scroll down the page, there is another upload file box for :

>WAR file to deploy
Select WAR file to upload

well, i guess i know too little about tomcat, as my qestion is too entry level.

thank you so much for your attention.


0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now