Вы находитесь на странице: 1из 2

About

Bookstore

RSS

Email

Follow us

Become a fan

J a v a E x a m p e ls
Home
You are here: Home

Learn Java Programming by Examples

1. Java SE API
Servlet

2. Java EE API

3. Frameworks

4. Other Libraries

5. Design Patterns

6. IDE / Tools

Search this site...


January 27, 2014 12:32 am

How do I get web application context path?

How do I get web application context path?


Posted by Wayan Saryada on May 3, 2008

The context path always comes first in a request URI. The path starts with a / character but does not end with a / character. When I have a web application with the URL like http://localhost:8080/myapps then myapps is the context path. For servlets in the default (root) context, this method returns .

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

package org.kodejava.example.servlet; import import import import import import javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; javax.servlet.ServletException; java.io.IOException; java.io.PrintWriter;

Find us on Facebook

Learn by Examples
Like 130 people like Learn by Examples.

public class ContextPathDemo extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // // HttpServletRequest.getContextPath() returns the portion // of the request URI that indicates the context of the // request. // String contextPath = req.getContextPath(); PrintWriter pw = res.getWriter(); pw.print("Context Path: " + contextPath); } }

F acebook social plugin

Register your servlet in the web.xml file and map it to the ctxpath as the url-pattern. Let say youve deployed your servlet into a web application named webapp then you can access your servlet using the following url: http://localhost:8080/webapp/ctxpath. Youll get the following information in your browser:

1 Context Path: /webapp

Related Posts
How do I get web application context path in JSP? How do I get my web application real path? How do I obtain ServletContext of another application? How do I read servlet context initilization parameters? How do I get servlet request URL information? How do I share object or data between users in web application? How do I read servlet init parameter?

Filed in: Servlet Tags: context path, servlet

About Wayan Saryada


I am a programmer, a runner, an open water diver and currently living in the island of Bali, Indonesia. View all posts by Wayan Saryada

Work from Home


Leave a Reply
Recent Projects
converted by Web2PDFConvert.com

Name (Required) Mail (will not be published) (Required) Website

A Java Minecraft Plugin Dev. java, software development

Min. 5,000
Develop Features for a Cryptocurrency Wallet/Client...

View

$750 - 1,500
Simple game with ranking for Android android, j2ee, java, mobile phone

View

$30 - 250
Find more projects

View

Post a Project
Powered by

Submit Comment

Advertise Here

Pages

Recent Posts

Recent Comments

Java Resources

About Bookstore

How do I create a simple mail client program in Swing? What are the system properties used for sending email? How do I send an HTML email? How do I read last n characters from a file? Java SE Installation Tutorial

Semih eker on How do I include a page fragment into JSP? Pierre on How do I send email using Gmail via TLS? Preethi on How do I move focus from JTextArea using TAB key? Amit on How do I get operating system temporary directory / folder? Srin on How to monitor file or directory changes?

Java Language Specification Java SE Technical Documentation Java Tutorials Java EE Technical Documentation Java EE 7 Tutorials

Affiliate Links

Shop Tech Titles on oreilly.com 50% off hosting plans from GoDaddy!

2014 Java Examples. All rights reserved.

converted by Web2PDFConvert.com

Вам также может понравиться