Servlet/jsp configuration details

Source: Internet
Author: User

Transfer from: dynamic network Production Guide www.knowsky.com

For example, how to configure environment variables and How to Run Servlet? There are too many such problems. Now I want to write a beginner entry, so that it can be helpful for beginners!

First, download the tool:

I suggest using editplus + JDK for beginners. I think it is easier to use editplus + JDK for beginners, such as JB, eclipse, and jcreator, but it does not know how to configure environment variables for beginners, in this way, it is difficult to reach the point of knowing and knowing why.

You can download it at the following address:

Editplus (the latest version is v2.11): http://count.skycn.com/softdown.php? Id = 3641 & url = http://sc-http.skycn.net/down/epp211a_cn.exe (you need to follow the registration code to find it yourself, a lot of online)

JDK (the latest version is java2sdk3164_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

Windows)

Then install JDK. I installed it under the c: \ JDK directory.

Then the classpath is set:

Just as the operating system uses path to search for executable programs, the Java Runtime Environment will traverse classpath to find classes. Even a simple program like helloworld, JVM will traverse every path defined by classpath, until the corresponding file is found.

I believe that if your system is not 2 K or XP, you should set the path as follows:

My computer-> properties-> advanced-> Environment Variables

Append the following path to the environment variable: C: \ JDK \ bin;.; C: \ JDK \ Lib.

You can also configure c: \ JDK \ bin;.; C: \ JDK \ Lib \ DT. jar; C: \ JDK \ Lib \ tools. jar.

★Remember: In the environment variables, remember not to be less. It indicates the current path. If there are fewer errors, you will say it!

DT. jar is a class library about the runtime environment, and tools. jar is a class library about some tools.

If c: \ JDK \ bin is not configured, "javac" is not an internal or external command, or a program or batch file that can be run ." This error occurs.

Let's write an example program:

Open editplus and create a new Java file. Follow the input below to keep it case sensitive:

Public class helloworld {

Public static void main (string [] ARGs ){

System. Out. println ("Hello, world! ");

}

}

Then save the file (CTRL +

S) to helloworld. Java, Java is case sensitive, so the case must be distinguished, that is, helloworld. Java is not helloworld. Java or other.

Run: Start-> Run-> cmd

Switch the directory to the current directory in the console:

Javac helloworld. Java

Java helloworld

You will see the output Hello, world on the console!

Javac is a compilation command that compiles helloworld. Java into helloworld. Class.

Java is the command to explain. JVM will explain and execute helloworld. Class.

At this time:

1. If exception in thread "Main" Java. Lang. noclassdeffounderror occurs:

Helloworld

That is, you didn't add that. (DOT) to the environment variable)

2. If exception in thread "Main" Java. Lang. nosuchmethoderror: Main

Or helloworld. Java: 1: Public class helloworld must be defined in a file

Called

"Helloworld. Java ".

That is, you did not write this helloworld case-insensitive, or you did not save it as helloworld. java. This name must be public

The class name is the same.

Let's talk about the problem of environment variables. Next, let's talk about how to compile and run the environment variables in editplus, and choose tools> parameter settings> Configure user tools.

1. Add a tool (add an Application)

Menu text: compile Java program

Program: C: \ JDK \ bin \ javac.exe

Parameter: File Name

Initial Directory: file directory

2. Add a tool (add an Application)

Menu text: run Java program

Program: C: \ JDK \ bin \ java.exe

Parameter: File Name (excluding the extension)

Initial Directory: file directory

You can add a tool group name as needed, such as debug Java program.

Then, in the drop-down menu of tools, you will see compile Java program and run Java

You can use Ctrl + 1 to compile and CTRL + 2 to run the program.

The following describes the servlet runtime environment:

To run servlet, JSP/servlet container is required. We recommend that you use Tomcat for beginners.

Tomcat (Latest Version 5.0): tomcat-5/nightly/jakarta-tomcat-5-bin-20030725.zip "> http://cvs.apache.org/builds/jakarta-tomcat-5/nightly/jakarta-tomcat-5-bin-20030725.zip

Decompress the package:

C: \ Tomcat

Then configure the environment variables. Add three system variables:

Java_home: C: \ JDK

Tomcat_home: C: \ Tomcat

Classpath: % java_home % \ Lib; % tomcat_home % \ Lib

The environment variables of Tomcat are configured. Check whether Tomcat can run as follows:

In the console, go to the C: \ Tomcat \ bin directory and run startup. Then, a window is displayed, which means that the server is running.

Enter http: // localhost: 8080 in the browser. If the welcome page appears, it means Tomcat is okay. Write your first Servlet as above.

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 ("<HTML>
Out. println ("this is my first servlet ");

Out. println ("</title>
Out. println ("
Out. println ("</body>
}

}

Then use javac helloworld. Java to compile the file. If the file cannot be imported javax. servlet .*

Then, copy the servlet. JAR file in c: \ Tomcat \ common \ Lib to c: \ JDK \ JRE \ Lib \ ext and compile it again!

Then, in the Tomcat directory c: \ Tomcat \ webapps \ Root, follow the following file structure:

Root \ index.html

Root \ Welcom. jsp

Root \ WEB-INF \ Lib \ myservlet. Jar (If your servlet. Class is typed into a. jar file, put it under Lib)

Root \ WEB-INF \ Classes \ helloworld. Class (put the above generated helloworld. Class file in this)

Then, enter http: // localhost: 8080/servlet/helloworld in the browser, and the server will return an error: error.

404 -- not found

What's going on?

Servlet must use c: \ Tomcat \ webapps \ Root \ WEB-INF directory under the web. xml file registration, open this web. xml file with EP, add in it:

<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>

Indicates the included servlet class. The following structure:

<Servlet-mapping>

<Servlet-Name> helloworld </servlet-Name>

<URL-pattern>/servlet/helloworld </url-pattern>

</Servlet-mapping>

Specifies the URL mode to which helloservlet maps.

After modifying web. XML, restart the server and then enter http: // localhost: 8080/servlet/helloworld. Then, a large Hello, world! Waiting for you.



Transfer from: dynamic network Production Guide www.knowsky.com

| Print this article

Click here to view all servlet content

Name: browsing all comments (4)

It is better to teach people to fish than to teach people to fish.

Search related content

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.