Jsp often see the beginners asked how to configure jsp tomcat, servlet and bean problem was summed up how to configure jsp tomcat, servlet and ben, beginners who want to help.
A development environment configuration
Step One: Download j2sdk and tomcat: The sun official station (http://java.sun.com/j2se/1.5.0/download.jsp) Download j2sdk, to download versions for Windows Offline Installation of the SDK, and the best download J2SE 1.5.0 Documentation, and then tomcat official site (http://jakarta.apache.org/site/downloads/downloads_tomcat-5.cgi) Download tomcat (download the latest 5.5.9 version of tomcat);
Step 2: installation and configuration of your j2sdk and tomcat: The Executive j2sdk tomcat and the installation procedures, and then click the default settings can be installed.
1. J2sdk installed, the need to configure some environment variables, in my computer -> Properties -> High -> environment variable -> add the following variables system environment variables (assuming your j2sdk installed in c: \ j2sdk1.5.0):
JAVA_HOME = c: \ j2sdk1.5.0
Classpath =.;% JAVA_HOME% \ lib \ dt.jar;% JAVA_HOME% \ lib \ tools.jar; (.; Must not less, because it represents the current path)
Path =% JAVA_HOME% \ bin
Then can write a simple procedure to test java J2SDK is installed successfully:
Public class Test (
Public static void main (String args []) (
System.out.println ( "This is a test program.");
)
)
The above procedures will be preserved for this document, entitled Test.java document.
Then open a command prompt window, cd to your Test.java directory, and type the following command
Javac Test.java
Java Test
If you see this printed This is a test program. So that the installation was successful, if not print this, you need to carefully check your configuration.
2. Installed Tomcat, in my computer -> Properties -> High -> environment variable -> add the following variables system environment variables (assuming your tomcat installed in c: \ tomcat):
CATALINA_HOME = c: \ tomcat
CATALINA_BASE = c: \ tomcat
Then amend the environment variables in the classpath, tomat installation directory under the common \ lib under (can be based on actual additional) servlet.jar added to the classpath, so as to modify the classpath as follows:
Classpath =.;% JAVA_HOME% \ lib \ dt.jar;% JAVA_HOME% \ lib \ tools.jar;% CATALINA_HOME% \ common \ lib \ servlet.jar;
Then can activate tomcat, visit http://localhost:8080 in IE, if that tomcat welcome pages so that the installation was successful.
Step 3: build their own directory jsp app
1. Tomcat to the installation directory webapps directory, we can see ROOT, examples, tomcat-docs like Tomcat onboard directory;
2. Webapps directory new directory name from myapp;
3.myapp a new directory under WEB-INF, attention, the directory name is case-sensitive;
4.WEB-INF under a new web.xml file, as follows:
<? Xml version = "1.0" encoding = "ISO-8859-1">
<! DOCTYPE web-app
PUBLIC "- / / Sun Microsystems, Inc / / DTD Web Application 2.3 / / EN"
"Http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name> My Web Application </ display-name>
<description>
A application for test.
</ Description>
</ Web-app>
5. Myapp under a new test jsp page document, entitled index.jsp, the contents of the documents are as follows:
<html>
<body>
<center>
Now time is: <% = new java.util.Date ()%>
</ Center>
</ Body>
</ Html>
6. Restart Tomcat
7. Opened browser, enter the current time http://localhost:8080/myapp/index.jsp see if description of the success.
Step 4: establish their own Servlet:
1. Most familiar with your editor (proposed use of the grammar checker java ide) a servlet new procedures, called Test.java document, the contents of the documents are as follows:
Package test;
Import java.io.IOException;
Import java.io.PrintWriter;
Import javax.servlet.ServletException;
Import javax.servlet.http.HttpServlet;
Import javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpServletResponse;
Public class Test extends HttpServlet (
Protected void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException (
PrintWriter out = response.getWriter ();
Out.println ( "<html> <body> <h1> This is a servlet test. </ H1> </ body> </ html>");
Out.flush ();
)
)
)
2. Compiler
Test.java will be on the c: \ test, the use of the following commands compiled:
C: \ Test> javac Test.java
Then in the c: \ Test will produce a compiler of the servlet documents: Test.class
3. Structural test \ Test.class shear% to% CATALINA_HOME \ webapps \ myapp \ WEB-INF \ classes, is that the shear test directory to the directory of classes, if classes directory does not exist on the new one. Now webapps \ myapp \ WEB-INF \ classes under test \ Test.class document directory structure
4. Laws webapps \ myapp \ WEB-INF \ web.xml, add servlet and servlet-mapping
Edit the web.xml shown below, add the contents of the red:
<? Xml version = "1.0" encoding = "ISO-8859-1">
<! DOCTYPE web-app
PUBLIC "- / / Sun Microsystems, Inc / / DTD Web Application 2.3 / / EN"
"Http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name> My Web Application </ display-name>
<description>
A application for test.
</ Description>
<servlet>
<servlet-name> Test </ servlet-name>
<display-name> Test </ display-name>
<description> A test Servlet </ description>
<servlet-class> Test.Test </ servlet-class>
</ Servlet>
<servlet-mapping>
<servlet-name> Test </ servlet-name>
<url-pattern> / Test </ url-pattern>
</ Servlet-mapping>
</ Web-app>
This passage in a statement that the servlet you want to call the Servlet, and servlet-mapping operated statement servlet "mapping" to the address / Test,
5. Well, restart Tomcat, start your browser, type http://localhost:8080/myapp/Test If you see the output This is a servlet test. Indicates that the preparation of the servlet success.
NOTE: revised the web.xml and added a new class must restart Tomcat
Step 4: Bean set up their own:
1. Most familiar with your editor (proposed use of the grammar checker java ide) a java new procedures, called TestBean.java document, the contents of the documents are as follows:
Package test;
(Public class TestBean
Private String name = null;
Public TestBean (String strName_p) (
This.name = strName_p;
)
Public void setName (String strName_p) (
This.name = strName_p;
)
Public String getName () (
Return this.name;
)
)
2. Compiler
TestBean.java will be on the c: \ test, the use of the following commands compiled:
C: \ Test> javac TestBean.java
Then in the c: \ Test will produce a compiler of the bean documents: TestBean.class
3. TestBean.class document will be to shear%% CATALINA_HOME \ webapps \ myapp \ WEB-INF \ classes \ test,
4. TestBean.jsp a new document, the contents of the documents:
<% @ Page import = "test.TestBean"%>
<html>
<body>
<center>
<%
TestBean testBean = new TestBean ( "This is a test java bean.");
%>
Java bean name is: <% = testBean.getName ()%>
</ Center>
</ Body>
</ Html>
5. Well, restart Tomcat, start your browser, the input output http://localhost:8080/myapp/TestBean.jsp If you see Java bean name is: This is a test java bean. Illustrated by the success of the Bean .
This completed the entire Tomcat under jsp, servlet and javabean configuration. Next needs to be done is more than reading, to read other people's good code, more hands to write their own code to enhance the development of their own in this regard the ability. |