Js|servlet beginners 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 use EDITPLUS+JDK, I think if use such as Jb,eclipse,jcreator, although the beginning of the time is more convenient, but it does not know how to configure the Novice door environment variables,
So that it is difficult to reach the point of knowing it.
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 (to follow the registration code on their own to find it, a lot of online)
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 forwindows)
Then the JDK is installed, and I'm putting it under the C:\JDK directory:
And then there's the question of classpath:
Just as the operating system uses path to search for executable programs, the Java runtime also traverses classpath to find classes, and even a simple program like HelloWorld, the JVM traverses
Classpath each path defined until the appropriate file is found.
Believe that you use the system is not 2k is XP, and then you should set the path as follows:
My Computer-> Properties-> Advanced-> Environment variables
Then append to the path behind the environment variable: C:\JDK\bin;. C:\JDK\lib
Can also be configured like this: C:\JDK\bin; C:\JDK\lib\dt.jar; C:\JDK\lib\tools.jar
★ Remember: In the environment variable. Remember not to be less, it represents the current path, if the error of the missing and so on will say!
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.
Then it's time to write the program:
First is (Helloworld.java), open the EditPlus, create a new Java file, please follow the following input, to a word without leakage, and distinguish case:
publicclasshelloworld{
Publicstaticvoidmain (String[]args) {
System.out.println ("hello,world!");
}
}
Then save this file (Ctrl+s) to Helloworld.java, remember that the case must be distinguished, is helloworld.java not Helloworld.java or other
Now it's time to run and start-> run->cmd
To switch directories to the current directory in the console:
Javachelloworld.java
Javahelloworld
You'll see the output hello,world! on the console. (not coming out?) I ate the computer:)
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 Exceptioninthread "main" Java.lang.NoClassDefFoundError:HelloWorld appears
That's what you don't add to the environment variables. (dot)
2. If Exceptioninthread "main" Java.lang.NoSuchMethodError:main appears
or helloworld.java:1:publicclasshelloworldmustbedefinedinafilecalled.
"Helloworld.java".
It's that you don't know the case. Write to this HelloWorld, or save time without saving as Helloworld.java
This name must be the same as Publicclass's name.
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: Compilejavaprogram
Program: C:\JDK\bin\javac.exe
Parameters: File name
Initial directory: File directory
2. Add tool (Add application)
Menu Text: Runjavaprogram
Program: C:\JDK\bin\java.exe
Parameters: File name (without extension)
Initial directory: File directory
The tool group name can be added casually, such as Debugjavaprogram
Then in the Tools Drop-down menu, you will see the Compilejavaprogram and Runjavaprogram options, and later you can use ctrl+1 to compile and ctrl+2 run the program.
The following is a discussion of the operation of the servlet:
To run the servlet first, you need Jsp/servletcontainer, and I recommend that beginners use Tomcat
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 variables:
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:\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
Enter http://localhost:8080 in the browser, and the Welcome interface appears, which means that Tomcat is fine.
And then, like the top, write your first servlet.
importjava.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
Publicclasshelloworldextendshttpservlet
Response.setcontenttype ("text/html");
Printwriterout=response.getwriter ();
Out.println ("Out.println ("Thisismyfirstservlet");
Out.println ("</title>Out.println ("Out.println ("</body>
}
}
And then use Javachelloworld.java to compile this file, if there is no importjavax.servlet.*
Then it should be 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.html
root\welcom.jsp
Root\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: Error404--notfound
What's going on?
The servlet must register with the Web.xml file C:\Tomcat\webapps\ROOT\WEB-INF this directory and use the EP to open the Web.xml file.
Join in the Inside
<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, congratulations
To settle:)
AI, the hands are tired, write it here, hope to have a little role for beginners!
================================
How to build a connection database application? For example, connect access, and Sqlsever.
Heard that there are JDBCJDBC-ODBC bridge and other ways. At present, my machine is only jdk1.4, I want to use JDBC way to install what kind of environment?
What if you want to drive a JDBC environment where you can go down? How is the program written?
--------------------------------
If you use a drive axle, you don't need to install anything else.
In
Control surface version--> management tools--> Data Source (ODBC)-->systemdsn
Add your database map, select the appropriate database driver
And then you can go through
JDBC:ODBC: Set the DSN name
Way to connect to the database.
----------------------------------
Privatestringjdbcdriver=null;
Privatestringjdbcurl=null;
Privatestringusername=null;
Privatestringpassword=null;
Publicdbconnect () {
Jdbcdriver= "Sun.jdbc.odbc.JdbcOdbcDriver";
Jdbcurl= "JDBC:ODBC:CBK";
Username= "System";
password= "Manager";
}
Publicconnectiongetconnection () {
Connectionconnection=null;
try{
Class.forName (Jdbcdriver);
Connection=drivermanager.getconnection (Jdbcurl,username,password);
}catch (Exceptione) {
System.out.println (e);
}
ReturnConnection;
}
}
This is the common way to connect! Just make a change.
--------------------------------------
It seems to be like this, first Rs.next ()
is Jdbc-odbc bridge bad? ^_^
There are several ways for Java to connect to a database:
According to the required different database drive points, divided into four kinds:
1:1 class drive. This is the way of Jdbc-odbc Bridge. However, this method is not suitable for the reuse and maintenance of the program, and is not recommended for use. The ODBC driver for the database is required.
2:2 class drive. This is the form of the jdbc+ vendor API. The manufacturer API is usually written in C, so this is not a long way to use.
3:3 class drive. This is the form of jdbc+ manufacturer Databaseconnectionserver+database.
This approach is the price between Java and database for a dedicated server to connect with the databases (typically provided by the database manufacturer). His advantage is that he can optimize the connection.
Class 4:4 Drive. This is the connection mode of pure jdbc+database. It is also the recommended way to connect. This makes application and database separate, developers only care about the implementation of internal logic without paying attention to the concrete implementation of the database connection. There are two ways to connect in this case:
Hard coding is the necessary parameter to be rigidly programmed into the database connection in the program.
Jndidatasource Way. It is in the external environment of the program is also called (context) set a DataSource data source, there is a jndi name, the program only need to find this name to get a database connection object.
=====================================
JSP connection oracle8/8i/9i database (in thin mode)
Testoracle.jsp is as follows:
<% @pagecontentType = "text/html;charset=gb2312"%>
<% @pageimport = "java.sql.*"%>
<body>
<%class.forname ("Oracle.jdbc.driver.OracleDriver"). newinstance ();
Stringurl= "Jdbc:oracle:thin: @localhost: 1521:ORCL";
ORCL for the SID of your database
Stringuser= "Scott";
Stringpassword= "Tiger";
Connectionconn=drivermanager.getconnection (Url,user,password);
Statementstmt=conn.createstatement (resultset.type_scroll_sensitive,resultset.concur_updatable);
Stringsql= "Select*fromtest";
Resultsetrs=stmt.executequery (SQL);
while (Rs.next ()) {%>
Your first field content is: <%=rs.getstring (1)%>
The contents of your second field are: <%=rs.getstring (2)%>
<%}%>
<%out.print ("Successful database operation, congratulations");%>
<%rs.close ();
Stmt.close ();
Conn.close ();
%>
</body>
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.