Getting started with programmers: talking about the configuration of servlet/jsp

Source: Internet
Author: User
Tags add count file copy net variables version variable thread
Js|servlet| Programming | What beginners often ask, such as: How to configure environment variables? How do I run the servlet? This problem appears a lot, now I write a beginner must read, in order to guide 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) and then the JDK is installed, 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 of the environment variable:

       
        
         
        C:\JDK\bin;.; C:\JDK\lib
       
        


You can also configure this:

       
        
         
        C:\JDK\bin;.; C:\JDK\lib\dt.jar; C:\JDK\lib\tools.jar
       
        


Remember that the "." In the environment variable should not be less, it represents the current path, and if there are fewer errors.

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

If not configured: C:\JDK\bin, 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.javajava 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 is you do not add that in the environment variable. (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:\JDK\bin\javac.exe

Parameters: File name

Initial directory: File directory

2. Add tool (Add application)

Menu Text: Run Java Program

Program: C:\JDK\bin\java.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:\jdktomcat_home:c:\tomcatclasspath:%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:\Tomcat\bin 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.*;p ublic class HelloWorld extends httpservlet{ public void doget (httpservletrequest request,httpservletresponse response) throwsservletexception,ioexception{ Response.setcontenttype ("text/html"); PrintWriter out = Response.getwriter (), Out.println ("  
         "); Out.println ("This is the My-my-a-Servlet"); o Ut.println ("  "); Out.println ("hello,world!"); Out.println (" ");}
       
        


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

Then it should be the C:\Tomcat\common\lib inside the Servlet.jar file copy to the C:\JDK\jre\lib\ext, compile again, there is no problem.

Then, in the C:\Tomcat\webapps\ROOT inside the Tomcat directory, press the following file structure:

       
        
         
        Root\index.htmlroot\welcom.jsproot\web-inf\lib\myservlet.jar (If your servlet's. Class hits the. jar file, put it under Lib) root\web-inf \classes\helloworld.class (Put the Helloworld.class file that was generated above in this)
       
        


Then enter Http://localhost:8080/servlet/HelloWorld in the browser, 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:\Tomcat\webapps\ROOT\WEB-INF This directory, open the Web.xml file with the EP, and add the following:

       
        
        
          
          
            HelloWorld 
           
          
            HelloWorld 
           servlet> 
           
           
             HelloWorld 
            
           
            /servlet/helloworld
             
           
           
         
        


Such a structure

       
        
        
          
          
            HelloWorld 
           
          
            HelloWorld 
           
         
       
        


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

       
        
        
          
          
            HelloWorld 
           
          
           /servlet/helloworld 
           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

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.