Implementing the first JSP and Servelt

Source: Internet
Author: User
Tags http post tomcat
JS 1. Download to the Sun home page to download the JDK installation version, assuming the JDK is installed in C:\j2sdk1.4.2_03\, then set the environment variable java_home as C:\j2sdk1.4.2_03\,
Then add C:\j2sdk1.4.2_03\bin to the Environment path variable and add Tools.jar and Dt.jar to the CLASSPATH environment variable if there is no classpath
environment variable, then create it.
2. Download the Tomcat installation version, assuming that Tomcat is installed in c:\tomcat4.1; Then set the environment variable catalina_home to c:\tomcat4.1;
C:\tomcat4.1\bin Add the PATH environment variable. When you install Tomcat, you are prompted to enter the admin password, which is later used to configure Tomcat
Need to use.
3. Execute Catalina_home\bin\startup.bat and start tomcat. Open IE browser, typing http://localhost:8080/(ensure port not and other services)
The "Tomcat administration" and "Tomcat Manager" two connections appear on the left side of the page.
4. Establishment of the Webroot directory in C:\, Webroot under the Web-inf directory, web-inf under the web.xml of this document. Edit Web.xml file, enter:
<?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>MyFirstWebSite</display-name>
<description>first Web </description>
</web-app>
and then save.
5. Click on the "Tomcat Manager" connection to enter the username password (admin,admin) entered during installation. This is the graphical interface for managing Tomcat.
6. Open Catalina_home/conf/server.xml, find <!--Tomcat Root context, and then join under it
<context path= "/webroot" docbase= "c:/webroot" debug= "0"/&GT;, save, restart Tomcat, build a c:/webroot under Index.html,
Content yourself, and then open ie, typing: http://localhost:8080/WebRoot/index.html, so that you can browse the HTML file you just created.
In this step, basically set up a separate site to run.


The following describes how to use the JSP and servlet.
1. We set up the helloworld.jsp under the C:/webroot, enter the following content:
<HTML>
<HEAD>
<TITLE>Hello!</TITLE>
<meta name= "generator" content= "EditPlus" >
<meta name= "Author" content= "" >
<meta name= "Keywords" content= "" >
<meta name= "Description" content= "" >
</HEAD>

<BODY>
<%out.println ("Hello world!"); %>
</BODY>
</HTML>
Save, then open the browser, typing: http://localhost:8080/WebRoot/HelloWorld.jsp, you can view the contents of the JSP output.

2. We create a servlet, named Myservlet1.java, placed below c:/webroot/src, and the input reads as follows:
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.io.*;
Import java.util.*;

/**
* <p>title: </p>
* <p>description: </p>
* <p>copyright:copyright (c) 2003</p>
* <p>company: </p>
* @author not attributable
* @version 1.0
*/

public class MyServlet1 extends HttpServlet {
private static final String Content_Type = "text/html;" CHARSET=GBK ";
Initialize Global Variables
public void Init () throws Servletexception {
}
Process the HTTP GET request
public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
Response.setcontenttype (Content_Type);
PrintWriter out = Response.getwriter ();
Out.println ("Out.println ("Out.println ("<body bgcolor=\" #ffffff \ ">");
Out.println ("<p>the Servlet has received a" + request.getmethod () +. This is the reply.</p> ");
Out.println ("</body>}
Process the HTTP Post request
public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
Doget (request, response);
}
Clean up resources
public void Destroy () {
}
}

3. Open the Web.xml (the location itself due to know), edited 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>MyFirstWebSite</display-name>
<description>first Web </description>

<servlet>
<servlet-name>myservlet1</servlet-name>
<servlet-class>MyServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet1</servlet-name>
<url-pattern>/myservlet1</url-pattern>
</servlet-mapping>

</web-app>
Save.

4. To compile the servlet:
Create lib directory under c:/webroot/web-inf/, copy C:\Tomcat 4.1\common\lib\servlet.jar below Lib directory.
Create a classes directory under c:/webroot/web-inf/.
Build a Complier.bat under c:/webroot/
The contents are as follows:
Javac-classpath./web-inf/lib/servlet.jar./src/*.java-d./web-inf/classes
Pause
Run Complier.bat
Restart Tomcat, open the browser inside to Http://localhost:8080/WebRoot/myservlet1, execute, if shown
"The servlet has received a get. This is the reply. Indicates a successful execution.





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.