Complete implementation of the "Head first Servlets and JSP" Mini Mvc:jardownload

Source: Internet
Author: User

1, first, write a download.html put to D:\apache-tomcat-7.0.77\webapps\JarDownload-v1.

<!DOCTYPE HTML><HTML>    <Body>        <formAction= "Jardownload.do"Method= "Get">            <BR/>Extract Code:<inputtype= "text"name= "passwd" /><BR/>            <BR/>            <inputtype= "Submit" />        </form>                <BR/>        <P>The extraction code is 123456.</P>    </Body></HTML>

2. Launch Tomcat and test the page through the browser.

Execute command line instruction first d:\apache-tomcat-7.0.77\bin>startup.sh

Then open the browser and enter url:http://localhost:8080/jardownload-v1/download.html

3, write Web. xml and test, put to D:\apache-tomcat-7.0.77\webapps\JarDownload-v1\WEB-INF, it is best to restart Tomcat.

<?XML version= "1.0" encoding= "Iso-8859-1"?><Web-appxmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"version= "2.4">    <servlet>        <Servlet-name>Test</Servlet-name>        <Servlet-class>Com.example.web.JarDownload</Servlet-class>    </servlet>    <servlet-mapping>        <Servlet-name>Test</Servlet-name>        <Url-pattern>/jardownload.do</Url-pattern>    </servlet-mapping></Web-app>

4. Prepare a test jar package and place it in D:\APACHE-TOMCAT-7.0.77\WEBAPPS\JARDOWNLOAD-V1

5. Write a servlet with full name com.example.web.JarDownload, compile it into a. class file and deploy to D:\apache-tomcat-7.0.77\webapps\JarDownload-v1\ Web-inf\classes\com\example\web

 PackageCom.example.web;Importjavax.servlet.*;Importjavax.servlet.http.*;ImportJava.io.*; Public classJardownloadextendsHttpServlet { Public voidDoget (httpservletrequest req, HttpServletResponse resp)throwsIOException, servletexception {resp.setcontenttype ("Application/jar");//things you want your browser to knowServletContext CTX=Getservletcontext (); InputStream is= Ctx.getresourceasstream ("/hello.jar"); intRead = 0; byte[] bytes =New byte[1024]; OutputStream OS=Resp.getoutputstream ();  while(read = Is.read (bytes))! =-1) {os.write (bytes,0, read); } //The jar packet is read into memory before being transcribed into the output stream. Os.flush ();    Os.close (); }}

6. Finally, test whether the jar package can be downloaded via the Web page.

7, after inspection, found that jardownload.do is really Hello.jar (just the name is different), but the file name is Url-pattern, modify the HTML and Web. XML can be.

This shows the benefits of mapping the logical name to the servlet file.

Complete implementation of the "Head first Servlets and JSP" Mini Mvc:jardownload

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.