Ubuntu tomcat7+eclipse Development, deployment servlet----development

Source: Internet
Author: User
Tags tomcat

N days, finally rely on their own groping + data, to solve the Ubuntu deployment of the servlet problem.

Originally downloaded on the Apache official website Tomcat8, according to the online tutorial, wrote a test of the servlet applet, but always not run up. Later think it might be written by themselves, the Web. xml format is not correct, so consider or take a servlet development environment, modify the Web. XML is more than its own write more.

It would be nice to see the myeclipse on the web, but I didn't find the myeclipse for Linux64 download link, and Eclipse is already installed, anyway Eclipse is so powerful, Simply build a development environment in eclipse.

The eclipse installation process does not repeat, go to http://www.eclipsetotale.com/tomcatPlugin.html download a tomcat plugin, found that plug-ins can only support to TOMCAT7 and Eclipse4.4, Check out the eclipse version, it's 4.4.1, okay. So delete the tomcat8, with Apt-get install TOMCAT7 directly installed TOMCAT7, very convenient.

Plug-in download good, unzip it. The jar file is moved to the plugin under the Eclipse directory (if you don't remember where Eclipse is located, you can find it with Find/-name eclipse). Open Eclipse (if you have already opened it and need a reboot), you can find more tomcat in Window-preferences. With the corresponding Tomcat version selected, the Tomcat home is set to/var/lib/tomcat7,context declaration mode is selected as Server.xml and the path is set to/etc/tomcat7/server.xml ( As for the context files, you can refer to Tomcat7 's doc inside the Deploy section of the study, there is not much research here.

The development environment is a complete construction, it is relatively simple, next test, create a new Web service project, automatically generated XML, and wrote a simple Test.helloservlet program:

 package test; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse; public class HelloServlet extends HttpServlet {@Override protected void doget (HttpServletRequest req, httpservletrespons E resp) throws Servletexception, IOException {printwriter pw = Resp.getwriter (); Pw.println ("Welcome to my first servlet
");
Pw.flush ();
Pw.close ();  } @Override protected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException
{Super.dopost (req, resp);}} 

The Javax.servlet class was not found during the period, because this class was in the Java EE, and I installed the J2SE. No problem, this class after installing TOMCAT7, can be found in the/usr/share/tomcat7/lib below the Servlet-api.jar, copy the jar file to the Java directory below the Jre/lib/ext, can be recognized, But always prompt for errors. You can use this method by right-clicking the Java resources->libraries->jre System library,build path under the Web Service project in the eclipse that you just created, Add the package you just copied as a external jar and it's OK.

Modify the Web. Xml inside the Webcontent/web-inf, adding

<servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>test.helloservlet </servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>hello</ servlet-name>
    <url-pattern>/hello</url-pattern>
</servlet-mapping>

These paragraphs, click to run, simple configuration of a preview server. This prompted port 8080 to be occupied because the Apt-get installed Tomcat automatically opened the service to 8080 ports occupied. Use the command service TOMCAT7 stop to turn Tomcat off, run again, the browser opens automatically, and the ' Welcome to my first servlet is successful.

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.