The first is to download tools:
I recommend beginners to use Editplus + JDK. I think if you use JB, Eclipse, JCreator, for example, although it is more convenient at the beginning, it will make it difficult for beginners to know how to configure environment variables, making it difficult to know what is going on. , to the point where you know 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 JDK (the 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 install the JDK. I installed it in the c:JDK directory.
Then there is the issue of setting CLASSPATH:
Just as the operating system uses PATH to search for executable programs, the Java running environment will also traverse CLASSPATH to find classes. Even for a simple program like HelloWorld, the JVM will traverse every path defined by CLASSPATH until until the corresponding file is found.
I believe that the system you are using is either 2k or XP. Then you should set the Path as follows:
My Computer->Properties->Advanced->Environment Variables
and then append after the Path of the environment variable: C:JDKbin;.;C: JDKlib
can also be configured like this: C:JDKbin;.;C:JDKlibdt.jar;C:JDKlibtools.jar
★Remember: in the environment variables. Remember not to Less, it indicates the current path. If there is less error, we will tell you later!
dt.jar is a class library about the running environment, and tools.jar is a class library about some tools.
If there is no configuration: C:JDKbin, "javac'" is not an internal or external command, nor is it an operable program. or batch file." error.
Let's write a sample program below:
open Editplus, create a new Java file, please enter as follows, be sure to remember every word, and distinguish between uppercase and lowercase letters:
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello,World!");
}
}
Then save this file (ctrl + s) to HelloWorld.java. Java is case-sensitive, so the case must be distinguished. It is HelloWorld.java, not helloworld.java or other.
Run: Start->Run->cmd
to 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 the compilation command, which compiles HelloWorld.java into HelloWorld.class.
java is the interpretation command. The JVM interprets and executes HelloWorld.class.
At this time:
1. IfException in thread "main" java.lang.NoClassDefFoundError: HelloWorld
That means you didn't add that in the environment variable. (dot)
2. If an Exception in thread "main" java.lang.NoSuchMethodError: main occurs
Or HelloWorld.java:1: Public class helloworld must be defined in a file called
"HelloWorld.java".
That is, you write this HelloWorld without distinguishing the case, or you do not save it as HelloWorld.java when saving. This name must be the same as the name of the public class.
That’s it for the issue of environment variables. Next, I will first talk about how to compile and run in Editplus. In Tools->Parameter Settings->Configuring User Tools
1. Add Tools (Add Application)
Menu text: Compile Java Program
: C:JDKbinjavac.exe
Parameters: File name
Initial directory: File directory
2. Add tool (Add application)
Menu text: Run Java Program
: C: JDKbinjava.exe
parameters: file name (excluding extension)
initial directory: file directory
tool group name can be added at will, such as Debug Java Program.
Then in the drop-down menu of Tools, you will see the two options Compile Java Program and Run Java Program. In the future, you can use ctrl + 1 to compile and ctrl + 2 to run the program.
Let’s discuss the running environment of Servlet:
To run Servlet , you need a JSP/Servlet container. I recommend beginners to use Tomcat.
Tomcat (latest version 5.0): http://localhost:8080 . If the welcome interface appears, it means that there is no problem with Tomcat. Then 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><head><title>");
out.println("This is my first Servlet");
out.println("</title></head><body>");
out.println("<h1>Hello,World!</h1>");
out.println("</body></html>");
}
}
Then use javac HelloWorld.java to compile this file as usual. If it fails to import javax.servlet.*,
then you should copy the servlet.jar file in C:Tomcatcommonlib to C:JDKjrelib ext, compile again, and there will be no problem!
Then press the following file structure in C:TomcatwebappsROOT in the Tomcat directory:
ROOTindex.html
ROOTwelcom.jsp
ROOTWEB-INFlibMyServlet.jar (if your servlet's .class is typed into a .jar file, put it under lib)
ROOTWEB-INFclassesHelloWorld.class (put the HelloWorld.class file generated above in this)
and then enter http://localhost:8080/servlet/HelloWorld in the browser, so the Server reported the error as expected: Error 404--Not Found
What happened?
Servlet must be registered using the web.xml file under the directory C:TomcatwebappsROOTWEB-INF. Open the web.xml file with EP and add:
<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 specified included servlet class. And the following structure:
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/servlet/HelloWorld</url-pattern>
</servlet-mapping>
indicates specifying which URL pattern HelloServlet should be mapped to.