Use eclipse3 + lomboz3 + Tomcat to develop JSP -- 4. a servlet instance

Source: Internet
Author: User
This article mainly introduces how to use lomboz to develop jstl and El. It is best to first refer to my previous article "using eclipse3.0.1 + lomboz3.0.1 + atat5.0.28 to develop jsp". The environment configuration is similar to it, here is a brief introduction.

I. Required Software XML buddyHome http://xmlbuddy.com download this is an Eclipse plug-in that can help edit XML files. Install the plug-in. For more information, see install the Eclipse plug-in using links. II. Environment Configuration1. create a lomboz J2EE project with the Project name: "servlet sample"; fill in web modules: "servletweb"; add servers: "Tomcat 5.0.28 ", as my previous article introduces eclipse new project in more detail, here is a simple introduction. 2. directory structure F:/j2sdk JDK directory

D:/eclipse directory F:/Java/Jakarta-Tomcat 5.0.28 Directory D:/plug-in/lomboz.301 lomboz directory (I installed it in links mode here, D: /Eclipse/workspace/servlet sample the jstl sample project directory just created Make appropriate Configurations Based on the installed software.   3. automatically format the XML file. Choose "Eclipse Main Interface"> "window"> "Preferences"> "xmlbuddy"> "formatting". Select "two" tickets "in the right column.

 

4. To enable lomboz to automatically add parameters to the configuration file web. XML, you need to change the Web. xml namespace from the original web-app_2_3.dtd to the web-app_2_4.xsd ".

Open the Web. xml file and modify it as follows:

Servlet samples/servletweb/WEB-INF/Web. xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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 web-app_2_4.xsd"
    version="2.4">
   
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <error-page>
  <error-code>404</error-code>
  <location>/error.jsp</location>
 </error-page>
</web-app>  

 

 3. Compile an instance

Make sure you have prepared the preparations before you can achieve good results.

1. Right-click servletweb and create a servlet.

 

2. Enter the following information:

Next step:

 

3. Open the Web. xml file again and find that the parameter has been automatically added.

Servlet samples/servletweb/WEB-INF/Web. xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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 web-app_2_4.xsd"
    version="2.4">
   
   
    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>javamxj.servlet.HelloServlet</servlet-class>
        <init-param>
            <param-name>greeting</param-name>
            <param-value>Hello</param-value>
        </init-param>
        <init-param>
            <param-name>name</param-name>
            <param-value>javamxj blog</param-value>
        </init-param>
    </servlet>
   
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
   
   
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <error-page>
        <error-code>404</error-code>
        <location>/error.jsp</location>
    </error-page>
   
</web-app>
4. Modify helloservlet. Java as follows:

Servlet sample/src/javamxj/servlet/helloservlet. Java

package javamxj.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @ Author javamxj
*
* Use initialization parameters to display two strings in the output window
* ("Hello javamxj blog"),
* If the initialization parameter is not defined, use the default string
* ("Welcome servlet !").
* The initialization parameters are defined in the web. xml file.
*
*/
Public class helloservlet extends httpservlet {
 private String defaultGreeting;
 private String defaultName;
Public void Init (servletconfig config) throws servletexception {
// Obtain initialization parameters from web. xml
Super. INIT (config );

// Obtain the initialization parameters. If none, use the default string.
If (defagregreeting = getinitparameter ("greeting") = NULL)
Defaultgreeting = "welcome ";
If (defaultname = getinitparameter ("name") = NULL)
Defaultname = "servlet! ";
}
Protected void doget (httpservletrequest request,
Httpservletresponse response) throws servletexception, ioexception {

// First set the Document Type
Response. setcontenttype ("text/html; charset = GBK ");
// Obtain the output stream
Printwriter out = response. getwriter ();

Out. println ("<HTML> Out. println ("<body> Out. println (defagregreeting + "" + defaultname );
Out. println ("
 }
}
5. Save, start the server, deploy the servletweb module, open the browser window, and enter the address: http: // localhost: 8080/servletweb/Hello 6. initialization parameters are not used below. Open the Web. xml file and save the modification as follows. 7. redeploy the servletweb module and refresh the browser window. OK! The default string is successfully called. The next article describes how to use" Sysdeo eclipse Tomcat launcher plugin"Edit and debug JSP and Servlet, and talk about its similarities and differences with lomboz.
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.