In the previous section, we deployed a Web project using MyEclipse, but the deployment of a static Web project, let's learn to write a Dynamic Web project, which must be used to write a dynamic project: servlet.
Servlets are named by Sun, servlet = server + applets (applets represent small applications), and Servlets are small programs that run on the server side.
Write a realistic login servlet and deploy the access.
1. Write the login JSP page and submit it by post.
To access this JSP page:
2. Write Loginservlet to verify the login username and password by background.
PackageCom.njupt.demo;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classLoginservletextendsHttpServlet {/*** Constructor of the object. */ PublicLoginservlet () {Super(); } /*** Destruction of the servlet. <br>*/ Public voiddestroy () {Super. Destroy ();//Just puts "destroy" string in log//Put Your code here } /*** The Doget method of the servlet. <br> * * This method was called when a form have its tag value met Hod equals to get. * * @paramrequest the request send by the client to the server *@paramresponse The response send by the server to the client *@throwsservletexception If an error occurred *@throwsIOException If an error occurred*/ Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {response.setcontenttype ("Text/html"); PrintWriter out=Response.getwriter (); Out. println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > "); Out.println ("<HTML>"); Out.println ("); Out.println ("<BODY>"); Out.print ("This is"); Out.print ( This. GetClass ()); Out.println (", using the GET method"); Out.println ("</BODY>"); Out.println ("</HTML>"); Out.flush (); Out.close (); } /*** The DoPost method of the servlet. <br> * * This method was called when a form have its tag value me Thod equals to post. * * @paramrequest the request send by the client to the server *@paramresponse The response send by the server to the client *@throwsservletexception If an error occurred *@throwsIOException If an error occurred*/ Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {response.setcontenttype ("Text/html"); PrintWriter out=Response.getwriter (); Out. println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > "); Out.println ("<HTML>"); Out.println ("); Out.println ("<BODY>"); Out.print ("This is"); Out.print ( This. GetClass ()); Out.println (", using the POST method"); Out.println ("</BODY>"); Out.println ("</HTML>"); Out.flush (); Out.close (); } /*** Initialization of the servlet. <br> * *@throwsServletexception If an error occurs*/ Public voidInit ()throwsservletexception {//Put Your code here }}
As we build the servlet using MyEclipse, we need to focus on the methods of Doget and Dopost, deciding whether to use the Get or post method, and when we have finished creating the servlet, I observe that Web. XML found:
<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "2.4"xmlns= "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"> <servlet> <Description>This is the description of my EE component</Description> <Display-name>This is the display name of my EE component</Display-name> <Servlet-name>Loginservlet</Servlet-name> <Servlet-class>Com.njupt.demo.LoginServlet</Servlet-class> </servlet> <servlet-mapping> <Servlet-name>Loginservlet</Servlet-name> <Url-pattern>/servlet/loginservlet</Url-pattern> </servlet-mapping> <welcome-file-list> <Welcome-file>index.jsp</Welcome-file> </welcome-file-list></Web-app>
The servlet registration and servlet mappings are done, as we mentioned earlier.
I'll write the Loginservlet, rewrite the Dopost method, and write the action target and commit method in index.jsp.
PackageCom.njupt.demo;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classLoginservletextendsHttpServlet {/*** Constructor of the object. */ PublicLoginservlet () {Super(); } /*** Destruction of the servlet. <br>*/ Public voiddestroy () {Super. Destroy ();//Just puts "destroy" string in log//Put Your code here } /*** The Doget method of the servlet. <br> * * This method was called when a form have its tag value met Hod equals to get. * * @paramrequest the request send by the client to the server *@paramresponse The response send by the server to the client *@throwsservletexception If an error occurred *@throwsIOException If an error occurred*/ Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {doPost (request,response); } /*** The DoPost method of the servlet. <br> * * This method was called when a form have its tag value me Thod equals to post. * * @paramrequest the request send by the client to the server *@paramresponse The response send by the server to the client *@throwsservletexception If an error occurred *@throwsIOException If an error occurred*/ Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {String username= Request.getparameter ("username"); String Password= Request.getparameter ("Password"); Response.setcontenttype ("Text/html"); PrintWriter out=Response.getwriter (); if("Test". Equals (username) && "123456". Equals (password)) {out. println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > "); Out.println ("<HTML>"); Out.println ("); Out.println ("<BODY>"); Out.print ("Sucess"); Out.println ("</BODY>"); Out.println ("</HTML>"); } Else{out. println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > "); Out.println ("<HTML>"); Out.println ("); Out.println ("<BODY>"); Out.print ("Error"); Out.println ("</BODY>"); Out.println ("</HTML>"); } out.flush (); Out.close (); } /*** Initialization of the servlet. <br> * *@throwsServletexception If an error occurs*/ Public voidInit ()throwsservletexception {//Put Your code here }}
That is, if you enter the correct username and password, the sucess will be displayed, otherwise output error.
Deployment Project
The results are as follows:
Input is correct:
This chapter concludes.
http://blog.csdn.net/jiuqiyuliang/article/details/36424981
MyEclipse Building a Java Web project: The Servlet "II"