Vous êtes sur la page 1sur 3

About

Bookstore

RSS

Email

Followus

Becomeafan

JavaExamples
LearnJavaProgrammingbyExamples

Home

1.JavaSEAPI
Servlet

2.JavaEEAPI

3.Frameworks

4.OtherLibraries

5.DesignPatterns

6.IDE/Tools

Searchthissite...
January21,20142:58pm

Youarehere:Home

HowdoIgetservletrequestURLinformation?

HowdoIgetservletrequestURLinformation?
PostedbyWayanSaryada onJuly2,2007

Intheexamplebelowweextractinformationabouttherequestobjectpathinformation.Weextracttheprotocoluser,serverandandits assignedportnumber.Weextractourapplicationcontextpath,servletpath,pathinfoandthequerystringinformation.Ifwecombaineall theinformationbelowwellgetsometingequalstotherequest.getRequestURL(). 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 package org.kodejava.example.servlet; importjava.io.IOException; importjava.io.PrintWriter; importjavax.servlet.ServletException; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; public class ServletUrlInformation extends HttpServlet { protected void doGet(HttpServletRequestrequest, HttpServletResponseresponse) throwsServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequestrequest, HttpServletResponseresponse) throwsServletException, IOException { // //GettingservletrequestURL // String url = request.getRequestURL().toString(); // //Gettingservletrequestquerystring. // String queryString = request.getQueryString(); // //Gettingrequestinformationwithoutthehostname. // String uri = request.getRequestURI(); // //Belowweextractinformationabouttherequestobjectpath //information. // String scheme = request.getScheme(); String serverName = request.getServerName(); int portNumber = request.getServerPort(); String contextPath = request.getContextPath(); String servletPath = request.getServletPath(); String pathInfo = request.getPathInfo(); String query = request.getQueryString(); response.setContentType("text/html"); PrintWriterpw = response.getWriter(); pw.print("Url:" + url + ""); pw.print("Uri:" + uri + ""); pw.print("Scheme:" + scheme + ""); pw.print("ServerName:" + serverName + ""); pw.print("Port:" + portNumber + ""); pw.print("ContextPath:" + contextPath + ""); pw.print("ServletPath:" + servletPath + ""); pw.print("PathInfo:" + pathInfo + ""); pw.print("Query:" + query); } }

Find us on Facebook

Learn by Examples
Like

119 people like Learn by Examples.

Facebook social plugin

Registertheservletintheweb.xmlfileanddefinetheurlpatterntourlinfointheservletmapping.Whenyouaccessthis servletusingthefollowingurlhttp://localhost:8080/urlinfo?x=1&y=1,youllgetthefollowingoutputonyourbrowser: 1 2 3 4 5 6 7 8 9 Url: http: //localhost:8080/urlinfo Uri: /urlinfo Scheme: http ServerName: localhost Port: 8080 ContextPath: ServletPath: /urlinfo PathInfo: null Query: x=1&y=1

Generated with www.html-to-pdf.net

RelatedPosts

Page 1 / 3

8 PathInfo: null 9 Query: x=1&y=1

RelatedPosts

HowdoIgetservletrequestheadersinformation? HowdoIgetparameternamesfromservletrequest? HowdoIcheckifparameterisexistsinservletrequest? HowdoIreadservletinitparameter? HowdoIreadrequestparameterfromservlet? HowdoIsendacookieinServlet? HowdoIreadcookieinServlet?

Filedin:Servlet

Tags: HttpServletRequest,servlet,url

AboutWayanSaryada
Iamaprogrammer,arunner,anopenwaterdiverandcurrentlylivingintheislandofBali,Indonesia. ViewallpostsbyWayanSaryada

LeaveaReply
Name(Required) Mail(willnotbepublished)(Required) Website

WorkfromHome
RecentProjects SubmitComment IndeedAPIintegrationandsearchbarfix(Java, PHPandWordpress)...

30250
J2EE,Java&SQLdevelopment.repost j2ee,java,sql

View

$1515/hr

View

Weneedaseriousprogrammerwhocancode Java&JavascriptinLiferayopentobidding<...

$30250
Findmoreprojects

View

PostaProject
Poweredby

Pages
About Bookstore

RecentPosts
HowdoIcreateasimplemail clientprograminSwing? Whatarethesystem propertiesusedforsending email? HowdoIsendanHTML email? HowdoIreadlastncharacters fromafile?

RecentComments
PierreonHowdoIsendemail usingGmailviaTLS? PreethionHowdoImove focusfromJTextAreausing TABkey? AmitonHowdoIgetoperating systemtemporarydirectory/ folder? SrinonHowtomonitorfileor directorychanges? SrinonHowtomonitorfileor directorychanges?

JavaResources
JavaLanguageSpecification JavaSETechnicalDocumentation JavaTutorials JavaEETechnicalDocumentation JavaEE7Tutorials

AffiliateLinks
ShopTechTitlesonoreilly.com

Generated with www.html-to-pdf.net

JavaSEInstallationTutorial

Page 2 / 3
50%offhostingplansfromGoDaddy!

email? HowdoIreadlastncharacters fromafile? JavaSEInstallationTutorial

systemtemporarydirectory/ folder? SrinonHowtomonitorfileor directorychanges? SrinonHowtomonitorfileor directorychanges?

AffiliateLinks
ShopTechTitlesonoreilly.com 50%offhostingplansfromGoDaddy!

2014JavaExamples.Allrightsreserved.

Generated with www.html-to-pdf.net

Page 3 / 3

Vous aimerez peut-être aussi