File name: "Simpleservlet.java"
Package cn.mldn.lxh.servlet;//Defining packages
Import java.io.*;
HttpServlet belongs to the Javax.servlet.http package
Servletexception belongs to the Javax.servlet package
Import javax.servlet.*;//Importing HttpServlet-owned packages
HttpServletRequest, HttpServletResponse stored under the Javax.servlet.http package
importjavax.servlet.http.*;
public class Simpleservlet Extendshttpservlet
{
Represents processing a GET request
public void doget (HttpServletRequest req,httpservletresponse resp) throws Ioexception,servletexception//Throw exception
{
PrintWriter out = Resp.getwriter ();//Instantiate out object.
Out.println ("<HTML>");
Out.println ("<HEAD>");
Out.println ("<title>the firstservlet</title>");
Out.println ("</HEAD>");
Out.println ("<BODY>");
OUT.PRINTLN ("
Out.println ("</BODY>");
Out.println ("</HTML>");
Out.close ();
}
public void DoPost (HttpServletRequest req,httpservletresponse resp) throws Ioexception,servletexception
{
This.doget (Request,response);
}
};
///////////////////////////////////////////////////////////////////////////////////////////
The JSP function is consistent with the servlet, which means that the servlet can be accessed externally, so access to it requires an address, so only the address map of the web is resolved.
How to do address mapping?
By modifying the Web. xml file.
The code is as follows:
<servlet>
<servlet-name>simple</servlet-name>//the servlet application name we defined
<servlet-class>cn.mldn.lxh.servlet.simpleservlet</servlet-class>//the specific servlet file whose name corresponds to the servlet we define
</servlet>
<servlet-mapping>//Address mapping
<servlet-name>simple</servlet-name>//the servlet application name we defined
<url-pattern>/demo</url-pattern>//Address Name
</servlet-mapping>
////////////////////////////////////////////////////////////////////////////////////////////
The purpose of the address mapping is to use the servlet, which works by:
Working process:
Input address: Http://localhost:8080/demo, through which to find the file name inside the mapping files simple, through simple to find the corresponding <servlet-name>simple</servlet-name And then navigate to this servlet file: Cn.mldn.lxh.servlet.SimpleServlet
This shows that,<servlet-name>simple</servlet-name> in the name of simple does not necessarily have to be consistent with the servlet filename "Simpleservlet.java", It is only when configuring the Web. xml file that we apply the name definition to the servlet by <servlet-class>cn.mldn.lxh.servlet.simpleservlet</ Servlet-class> We can navigate to this servlet file.
Create a Java program like a normal project, but this program is Extendsservlet, and there is no master method
public class Helloworldservlet extends httpservlet{
@SuppressWarnings ("deprecation")
@Override
protected void Doget (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
Resp.setcontenttype ("text/html");
PrintWriter out = Resp.getwriter ();
Out.println ("Out.println ("<body>baliang ai Banjin, it ' s right!" +new Date (). toLocaleString () + "</body>
Out.flush ();
}
}
The last line within the Webroot->web-inf->web.xml is modified as follows:
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>//This is the Servlet's class name
<servlet-class>MyEclipse.HelloWorldServlet</servlet-class>//Full class name
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>//This is the same as the first one above, for the servlet class name
<url-pattern>/HelloWorldServlet</url-pattern>//must have A/Plus class name This is for the URL in the folder/test after the subdirectory, By this correspondence to get the name of the above Servlet-name, Helloworldservlet, and then by this name to get <servlet> within the <servlet-name> (and sub-correspondence), so as to get < Servlet-class> instantiation of a Myeclipse.helloworldservlet class
</servlet-mapping>
</web-app>//last line
Java Web. XML Configuration Servlet