A servlet in Java is actually a class that extends the performance of the server, which resides in applications that can be accessed through the request-response programming model. A servlet can respond to any type of request, but it is typically used only to extend the application of a Web server. Java servlet Technology defines an HTTP-specific servlet class for these applications, and the base class is HttpServlet. The servlet itself is a life-cycle, interested in searching for other articles to implement Java servlet and HTTP communication, and this article requires the next JDK (http://www.oracle.com/technetwork/java/javase/ downloads/index.html)
, Tomcat (Specific procedure can refer to the previous article Mac Apache Tomcat installation configuration), Ecliplse for Java EE (http://www.eclipse.org/downloads/)
1.Eclipse Downloads
2. Start Eclipse and create a new Java Web project;
3. Basic project settings, set up the Tomcat version to run, set the servlet version;
If there is no special demand for direct finish, the final directory is as follows, Java resources in the SRC directory settings storage class files, WebContent can store JSP and HTML files, note that Web-inf without web. XML, this is the new version of the feature;
4. If the third step does not have a direct finish, then the next two steps should be the following:
Can check whether the need for Web. XML, this article does not need to choose according to their own circumstances;
Create a new HTML or JSP file under 5.WebContent:
The code in the HTML is as follows:
<! DOCTYPE html>
Run Address http://localhost:8080/WebDemo/FirstDemo.html
JSP page run similar, there is no demonstration, the next introduction to the servlet;
6.Servlet file
Set up a direct finish, do not set the package name;
The code in Firstservlet.java is as follows:
Import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.annotation.WebServlet; Import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** * Servlet Implementation class Firstservlet */@WebServlet ("/firstservlet" public class Firstservlet extends HttpServlet {private static final long serialversionuid = 1L; /** * @see httpservlet#httpservlet () */public Firstservlet () {super (); TODO auto-generated Constructor stub}/** * @see Httpservlet#doget (httpservletrequest request, HttpServletResponse R esponse) */protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//TODO auto-generated method stub}/** * @see httpservlet#dopost (httpservletrequest request, HttpServletResponse response) */protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexception {//TODO auto-generated method stub}}
Add an output code to the 7.doGet method, and your browsing will see the effect you should have:
Response.setcontenttype ("Text/html;charset=utf-8"); PrintWriter out = Response.getwriter (); Out.println ("<! DOCTYPE html> "); Out.println ("<Html>"); Out.println ("<Head><title>FirstServlet</title></Head>"); Out.println ("<Body>"); Out.print ("firstservlet----"); Out.println ("Flyelephant (Http://www.cnblogs.com/xiaofeixiang)"); Out.println (" </Body>"); Out.println ("</Html>"); Out.flush (); Out.close ();
The effect is as follows:
8. The 6th step if you do not directly finish,next after the two selection options are as follows:
Select the method of implementation:
Mac under the simple Java Web development process is completed, there is not much to preach, to meet the basic use, if there is a problem, welcome to the private messages I or comment area Exchange ~
Reference: 3.0 features of the servlet (http://www.ibm.com/developerworks/cn/java/j-lo-servlet30/#major3)
Java servlet and HTTP communication in Mac OS