Learning the web under Java EE (Developing Dynamic Web Engineering in Eclipse, setting up the servlet environment, and some of the servlet's methods)

Source: Internet
Author: User

An easy way to implement Java EE version of Eclipse Development Dynamic Web Engineering (Javaweb project)
1. Switch the development options to Java EE
2. You can find the package Explorer in window-"Shou view and drag it to the left of the development zone.

(Tomcat environment is not built here to say much)
3. Create a new Tomcat server in the servers panel, and be sure to link to the root directory of the Tomcat installation
4. Create a new dynamic Web Project. Where target runtime needs to choose Tomcat6.0 (3)
5. Developing Java Web applications
6. You can run the Web project by right-clicking the project and selecting run on server

--------------------------------------------------------------------------------------------------------------- ----------

Concrete steps for building a Dynamic Web project:

Open Java EE version of Eclipse, switch to javaee,file->new->dynamic Web project-> fill in the Dynamic Web project name->target Runtime selection Tomcat6.0 (3) ->finish;

In the SRC directory you can create Java classes, and packages, the following is the SRC directory set up the following classes:

Package com.lanqiao.javatest;

Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Enumeration;

Import Javax.servlet.Servlet;
Import Javax.servlet.ServletConfig;
Import Javax.servlet.ServletContext;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;

public class Hservlet implements servlet{
/*
* Create the first servlet:
* Servlet is an interface that runs on the server side of the Java skeleton;
* First create a servlet class constructor and then the We.xml configuration and mapping file under Web-inf
*
* servlet container: A software environment that runs Servlet,jsp,filter, etc.
* Can be used to create a servlet and invoke the servlet's related recurrence method;
*
* Methods of the servlet life cycle, life cycle related methods: The method is that the servlet container is responsible for invoking
* 1. Constructor: Called only once, only the first time the servlet is requested, the servlet instance is created, and the constructor is called, which indicates that the servlet is a single instance!
* 2.init method: Called only once, after the creation of a good instance is called immediately. Used to initialize the servlet;
* 3.servlet: is called multiple times, each request will invoke the Servlet method, which is actually used to invoke the request;
* 4.destroy method: Called only once, before the Web app where the current servlet resides is unloaded, to free resources occupied by the current servlet.
*
* LOAD-ON-STARTUP (configuration parameter): You can specify the time when the servlet was created;
* Configured in the servlet node;
* <servlet>
<servlet-name>secondServlet</servlet-name>
<servlet-class>com.lanqiao.javatest.SecondServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
* The smaller the value, the earlier it was created
*
* */
@Override
public void Destroy () {
System.out.println ("destroy");

}
/*
* ServletConfig: Encapsulates the configuration information of the servlet, and can obtain the ServletContext object, which has four methods;
* Accessory servlet initialization parameters, must be configured in front of Load-on-startup
* 1. Get initialization parameters
* Getinitparameter (String name): Gets the initialization parameter of the specified parameter name
* 2.getInitParameterNames (): Gets the enumeration object consisting of the parameter names.
* 3.getServletName (): method to get the name of the servlet (the name from the Web. xml)
*
* 4.getServletContext (): Gets servletcontext;context as context, gets the context of the servlet;
* ServletContext: Can be thought of as a big housekeeper of Web applications, where you can get information about all aspects of your current web app:
* (1): Gets the initialization parameters of the current Web application (used by all servlets)
* First configuration and acquisition (configured in Web. xml)
* (2): Gets the absolute path of a file for the current web App
* Getrealpath (String Path)
* (3): Gets the name of the current web app: Getcontextpath ()
* (4): Gets the input stream of a file object for the current web App
* getResourceAsStream ("jdbc.properties"): Files are files in src directory
* getResourceAsStream ("/web-inf/classes/jdbc.properties"): The second method
* */
@Override
Public ServletConfig Getservletconfig () {
System.out.println ("Getservletconfit");
return null;
}

@Override
Public String Getservletinfo () {
System.out.println ("Getservletinfo");
return null;
}

@Override
public void init (ServletConfig servletconfig) throws Servletexception {
System.out.println ("Init");
String user=servletconfig.getinitparameter ("user");
System.out.println ("User:" +user);

Enumeration<string> Names=servletconfig.getinitparameternames ();
while (Names.hasmoreelements ()) {
String name=names.nextelement ();
String Value=servletconfig.getinitparameter (name);
System.out.println ("Name:" +name+ "\ T" + "value:" +value);
}

method to get the name of the servlet (name from Web. xml)
String Servletname=servletconfig.getservletname ();
System.out.println (Servletname);

Gets the ServletContext object;
ServletContext Servletcontext=servletconfig.getservletcontext ();
String driver=servletcontext.getinitparameter ("Driver");
System.out.println ("Driver:" +driver);

Enumeration<string> Names2=servletcontext.getinitparameternames ();
while (Names2.hasmoreelements ()) {
String name=names2.nextelement ();
String Value=servletcontext.getinitparameter (name);
System.out.println ("Name:" +name+ "\ T" + "value:" +value);
}

Gets the absolute path of a file for the current web app (must be under the root directory: webcontent)
String Realpath=servletcontext.getrealpath ("/javaee.test");
System.out.println (Realpath);

Get the name of the current web app: Getcontextpath ()
String Contextpath=servletcontext.getcontextpath ();
System.out.println (ContextPath);

Gets the input stream of a file object for the current web app, where the file is created in the SRC directory
try {
ClassLoader Classloader=getclass (). getClassLoader ();
InputStream Is=classloader.getresourceasstream ("jdbc.properties");
System.out.println ("1:" +is);

} catch (Exception e) {
E.printstacktrace ();
}
The second method:
try {
InputStream Is2=servletcontext.getresourceasstream ("/web-inf/classes/jdbc.properties");
System.out.println ("2:" +is2);
} catch (Exception e) {
E.printstacktrace ();
}

}

@Override
public void Service (ServletRequest arg0, Servletresponse arg1) throws Servletexception, IOException {
System.out.println ("Servlce");

}
Public Hservlet () {
System.out.println ("HelloWorld");
}
}

-------------------------------------------------------------------------------------------------------------

To create a configuration file in Webcontent->web-inf->lib->web.xml (already existing), here are some of the configurations in Web. xml

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id= "Webapp_ ID "version=" 2.5 ">

<!--Configure initialization parameters for the current web app (can be written multiple, global)--
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>

<context-param>
<param-name>jdbcUrl</param-name>
<param-value>jdbc:mysql:///test</param-value>
</context-param>


<!--Configuring and mapping Servlets (must have)--
<servlet>
<servlet-name>HServletttt</servlet-name>
<servlet-class>com.lanqiao.javatest.SecondServlet</servlet-class>
<load-on-startup>5</load-on-startup>
</servlet>
<!--Map--
<servlet-mapping>
<servlet-name>HServletttt</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>


<servlet>
<servlet-name>seServlet</servlet-name>
<servlet-class>com.lanqiao.javatest.HServlet</servlet-class>

<!--Configure the servlet initialization parameters (local)--
<init-param>
<!--initialize parameter names, initialize parameter values--
<param-name>user</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>123lxn</param-value>
</init-param>
<!--can specify the timing of the servlet creation--
<load-on-startup>2</load-on-startup>

</servlet>

</web-app>

Learning the web under Java EE (Developing Dynamic Web Engineering in Eclipse, setting up the servlet environment, and some of the servlet's methods)

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.