Get the servlet absolute path and relative path code

Source: Internet
Author: User

Get the servlet absolute path and relative path code


Import java. io .*;
Import java. text .*;
Import java. util .*;
Import javax. servlet .*;
Import javax. servlet. http .*;

Public class MyServlet extends HttpServlet {

Public void doGet (HttpServletRequest req, HttpServletResponse res)
Throws ServletException, IOException {
Res. setContentType ("text/plain; charset = Shift_JIS ");
PrintWriter out = res. getWriter ();
Res. setHeader ("Content-Language", "ja ");

Locale locale = new Locale ("ja ","");
DateFormat full = DateFormat. getDateTimeInstance (DateFormat. LONG,
DateFormat. LONG,
Locale );
Out. println ("In Japan :");

Try {
FileInputStream FCM =
New FileInputStream (req. getRealPath ("/HelloWorld. ISO-2022-JP "));
InputStreamReader isr = new InputStreamReader (FCM, "ISO-2022-JP ");
BufferedReader reader = new BufferedReader (isr );
String line = null;
While (line = reader. readLine ())! = Null ){
Out. println (line );
}
}
Catch (FileNotFoundException e ){
E. printStackTrace ();
}

Out. println (full. format (new Date ()));
}
}
 
<? Xml version = "1.0" encoding = "iso-8859-1"?>
<! DOCTYPE web-app
PUBLIC "-// Sun Microsystems, Inc. // DTD Web Application 2.3 // EN"
Http://java.sun.com/dtd/web-app_2_3.dtd>
<Web-app>

<Servlet> <servlet-name> MyServletName </servlet-name>
<Servlet-class> MyServlet </servlet-class>
</Servlet>

<Servlet-mapping> <servlet-name> MyServletName </servlet-name>
<Url-pattern> *. htm </url-pattern>
</Servlet-mapping>

</Web-app>
 
Identify file path information

Import java. io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;

Public class MyServlet extends HttpServlet {

Public void doGet (HttpServletRequest req, HttpServletResponse res)
Throws ServletException, IOException {
Res. setContentType ("text/plain ");
PrintWriter out = res. getWriter ();

If (req. getPathInfo ()! = Null ){
Out. println ("The file" "+ req. getPathInfo () + """);
Out. println ("Is stored at" "+ req. getPathTranslated () + """);
}
Else {
Out. println ("Path info is null, no file to lookup ");
}
}
}
 
<? Xml version = "1.0" encoding = "ISO-8859-1"?>

<! DOCTYPE web-app
PUBLIC "-// Sun Microsystems, Inc. // DTD Web Application 2.2 // EN"
Http://java.sun.com/j2ee/dtds/web-app_2_2.dtd>

<Web-app>
<Servlet> <servlet-name> MyServletName </servlet-name>
<Servlet-class> MyServlet </servlet-class>
</Servlet>

<Servlet-mapping> <servlet-name> MyServletName </servlet-name>
<Url-pattern>/index.html </url-pattern>
</Servlet-mapping>
</Web-app>
 

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.