Servlet(Server Applet), full nameJava Servlet, no Chinese translation is available. is to useJAvawritten byServerEndprogram. Its main function is to interactively browse and modify data, generate dynamicWebcontent. The narrow servlet refers to the Java language implementation of aInterface, a generalized servlet refers to any class that implements the Servlet interface ., it is generally understood that the servlet is the latter.
The servlet runs in a Java-enabled application server. From the implementation, the servlet can respond to any type of request, but in most cases the servlet is used only to extend the HTTP protocol-based Web server.
Once we know the servlet, we can write a servlet of our own:
File name is Servlettest.java:
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class servlettest Extends httpservlet{ private static final string content_type = "text/ HTML; CHARSET = GBK "; public void init () throws Servletexception{ } public void doget (Httpservletrequest request, Httpservletresponse response) throws ServletException,IOException{ Response.setcontenttype (Content_Type); printwriter out= response.getwriter (); out.println (""); out.println (""); out.println (""); out.println (" This is the GET request in response to the servlet "); out.println (" "); out.println (" "); out.close (); } public void dopost (httpservletreqUest request,httpservletresponse response) throws ServletException,IOException{ response.setcontenttype (Content_Type); printwriter out= response.getwriter (); out.println (""); out.println (""); out.println (""); out.println (" This is the response to the servlet's post request "); out.println (" "); out.println (" "); out.close (); } public void destroy () { } }
use CMD to compile him into the Servlettest.class class (where it is important to note that we compile directly when theJava servlet program does not exist in the Compile Times error packet Javax.servlet ) How to solve it?
Locate the installation path for Tomcat, such as C:\Program files\apache-tomcat-8.0.0-rc5\l servlet-api.jarf copy to C:\ProgramFiles\ Java\jdk1.7.0_45\jre\lib\ext is your JDK installation path under the same directory (with the same) and then compile on the OK!
Questions about the Java servlet compilation