Getting started: very basic) servlet/jsp configuration detailed

Source: Internet
Author: User
Tags count file copy net variables variable thread tomcat
Js|servlet| explain the beginner asked such as: "How to configure the environment variable" How to run the servlet ah? Such a problem too much, now I write a beginner must read, in order to guide the beginners!

The first is the download tool:

I suggest beginners with EDITPLUS+JDK, I think if used for example Jb,eclipse,jcreator, although the beginning of the time is more convenient, but it does not know how to configure the novice door to the environment variables, so difficult to achieve know it, know the reason why.

Can be downloaded from the following address:

EditPlus (latest version is v2.11): http://count.skycn.com/softdown.php?id=3641&url=http://sc-http.skycn.net/down/epp211a _cn.exe JDK (Latest version is java2sdk1_4_2): http://count.skycn.com/softdown.php?id=3116&url=http://sc-http.skycn.net/ Down/j2sdk-1_4_2-windows-i586.exe (this is for Windows)

Then you install the JDK, and I'm putting it under the C:JDK directory.

Then set the classpath problem:

Just as the operating system uses path to search for executable programs, the Java runtime also traverses classpath to find classes, and even HelloWorld such a simple program, the JVM traverses every path defined by Classpath until it finds the appropriate file.

Believe that you use the system is not 2k is XP, and then you should set the path as follows:

My Computer-> Attributes-> Advanced-> Environment variables

Then append to the path behind the environment variable: c:jdkin;. C:jdklib

Can also be configured like this: C:jdkin; C:jdklibdt.jar; C:jdklib Ools.jar

★ Remember: In the environment variable. Remember that it is not less, it represents the current path, if the missing error will be said!

Dt.jar is a class library about the runtime environment, Tools.jar is a class library for some tools

If not configured: C:jdkin, the "Javac" is not an internal or external command, nor a running program or batch file. "Such a mistake.

Let's write a sample program:

To open EditPlus, create a new Java file, follow the words below, to keep a word, and to distinguish the case:

public class helloworld{
public static void Main (string[] args) {
System.out.println ("hello,world!");
}
}

Then save the file (Ctrl + S) to the Helloworld.java,java is case-sensitive, so the case must be distinguished, is helloworld.java not Helloworld.java or other.

Run: Start-> Run->cmd

To switch directories to the current directory in the console:

Javac Helloworld.java
Java HelloWorld

You'll see the output hello,world! on the console.

Javac is a compiler command that compiles Helloworld.java into Helloworld.class

Java is the interpretation of commands, and the JVM interprets Helloworld.class.

At this time:

1, if appear exception in thread "main" Java.lang.NoClassDefFoundError:HelloWorld
That's what you don't add to the environment variables. (dot)

2, if appear exception in thread "main" Java.lang.NoSuchMethodError:main
or helloworld.java:1: public class HelloWorld must is defined in a file called

"Helloworld.java".

It is that you do not distinguish between the case of writing this HelloWorld, or save time is not saved as Helloworld.java. The name must be the same as the name of public class.

To the problem of environment variable here, I first said how to compile and run in EditPlus, tools-> parameter Set-> Configure user tool

1. Add tool (Add application)

Menu Text: Compile Java Program

Program: C:jdkinjavac.exe

Parameters: File name

Initial directory: File directory

2. Add tool (Add application)

Menu Text: Run Java Program

Program: C:jdkinjava.exe

Parameters: File name (without extension)

Initial directory: File directory

Tool group names can be added casually, such as the Debug Java program.

Then, in the Tools Drop-down menu, you'll see the compile Java program and run Java programs, and then you can use CTRL + 1 to compile and ctrl +2 to run the application.

Here we discuss the environment in which the servlet is run:

To run the Servlet, you need Jsp/servlet container, and I recommend Tomcat for beginners.

Tomcat (Latest version 5.0): Http://cvs.apache.org/builds/jakarta-tomcat-5/nightly/jakarta-tomcat-5-bin-20030725.zip

Then extract the compressed package to:

C:tomcat

Then configure the environment variable; add three system variables:

Java_home:c:jdk
Tomcat_home:c:tomcat
CLASSPATH:%java_home%lib;%tomcat_home%lib

The Tomcat environment variable is configured to verify that Tomcat is able to run the following:

Go to the C:tomcatin directory in the console, run startup, and then return to a window, jump a bunch of things, and finally indicate that the server is running.

When you enter http://localhost:8080 in the browser, the Welcome interface appears, which means that Tomcat is fine. Then, like the above, write to your first servlet.

Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
public class HelloWorld extends HttpServlet
{
public void doget (HttpServletRequest request,httpservletresponse response) throws Servletexception,ioexception
{

Response.setcontenttype ("text/html");
PrintWriter out = Response.getwriter ();
Out.println ("Out.println ("This is the My A-Servlet");
Out.println ("</title> Out.println ("Out.println ("</body>
}
}

Then use Javac Helloworld.java to compile this file, if there is no import javax.servlet.*

Then it should be c:tomcatcommonlib inside the Servlet.jar file copy to the C:jdkjrelibext, compile again, there is no problem!

Then, in the C:tomcatwebappsroot inside the Tomcat directory, press the following file structure:

Rootindex.html
rootwelcom.jsp
Rootweb-inflibmyservlet.jar (If your servlet's. Class hits the. jar file, put it under Lib)
Rootweb-infclasseshelloworld.class (Put the Helloworld.class file that was generated above in this)

Then in the browser input http://localhost:8080/servlet/HelloWorld, so the server is expected to complain: error 404--not Found

What's going on?

The servlet must register using the Web.xml file c:tomcatwebappsrootweb-inf This directory, open the Web.xml file with the EP, and add the following:

<servlet>
<servlet-name> HelloWorld </servlet-name>
<servlet-class> HelloWorld </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> HelloWorld </servlet-name>
<url-pattern>/servlet/helloworld </url-pattern>
</servlet-mapping>

Such a structure

<servlet>
<servlet-name> HelloWorld </servlet-name>
<servlet-class> HelloWorld </servlet-class>
</servlet>

Represents the specified servlet class to include. And the following structure:

<servlet-mapping>
<servlet-name> HelloWorld </servlet-name>
<url-pattern>/servlet/helloworld </url-pattern>
</servlet-mapping>

Indicates which URL pattern the specified helloservlet should be mapped to.

After modifying the web.xml, restart the server, and then enter the Http://localhost:8080/servlet/HelloWorld, so a hello,world! waiting for you.

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.