Getting started must read: servlet/jsp configuration Super Detailed

Source: Internet
Author: User
Tags file copy connect sql mysql mysql client net string stmt
Js|servlet| detailed

Beginners ask such things as: How to configure environment variables? How do I run the servlet? Such a problem too much, now I write a beginner must read, I hope to be able 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)

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:\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 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:\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.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:\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:\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.

When you enter http://localhost:8080in 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 (" ");<br>Out.println ("This is the My A-Servlet");<br>Out.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 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 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:\Tomcat\webapps\ROOT\WEB-INF This directory, open the Web.xml file with the EP, and add the following:


HelloWorld
HelloWorld


HelloWorld
/servlet/helloworld


Such a structure


HelloWorld
HelloWorld


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


HelloWorld
/servlet/helloworld


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.

  Attached: JSP development environment Configuration whole process

There are a number of problems in configuring the JSP development environment, thank many predecessors experience summary of the article for me is simply to send carbon in the snow, in order to help beginners like me, here summed up the experience of many authors, thank them for their selfless dedication, but also hope that more people to carry forward this spirit.

The following software download link address can not be displayed, please go directly to the official website to download it!

Software downloads

Mysql

Download version: Mysql-4.1.13-win32.zip (Windows downloads)

  Http://dev.mysql.com/downloads/mysql/4.1.html

JDBC Driver

Download version: Mysql-connector-java-3.1.10.zip

  Http://dev.mysql.com/downloads/connector/j/3.1.html

Download J2SDK

Download version: Jdk-1_5_0_04-windows-i586-p.exe

  http://java.sun.com/j2se/1.5.0/download.jsp

Download Tomcat

Download version: Jakarta-tomcat-5.5.9.exe

  http://jakarta.apache.org/site/downloads/downloads_tomcat-5.cgi

Install, configure J2SDK:

Perform J2SDK setup, install by default, or customize the path, but modify the following configuration

Configure J2SDK:

To configure environment variables:

My Computer-> Properties-> the advanced-> environment variable-> system variable add the following environment variables:

Java_home=c:\program files\java\jdk1.5.0_04
Classpath=%java_home%\lib\dt.jar;
%java_home%\lib\tools.jar;
Path=%java_home%\bin;
%SystemRoot%\System32;
%SystemRoot%;
%systemroot%\system32\wbem


Write a simple Java program to test whether the J2SDK has been installed successfully:

public class Hello
{
public static void Main (String args[])
{
System.out.println ("Hello");
}
}


Save the program as a file with a file name of Hello.java.

Open a Command Prompt window and enter the directory where Hello.java is located, and type the following command:

Javac Hello.java
Java Hello


At this point if the print out Hello is installed successfully, if not printed out this sentence, carefully check the above configuration is correct.

Note The System Folder option should make sure that "Hide extensions for known file types" is not checked.

Install, configure Tomcat

Perform the Tomcat installer, install by default, or customize the path, but modify the following configuration:

My Computer-> Properties-> Advanced-> environment variable-> system variable add the following environment variables

Catalina_home=c:\program Files\apache
Software Foundation\tomcat 5.5
Catalina_base=c:\program Files\apache
Software Foundation\tomcat 5.5


Modify the Classpath in the environment variable, add the Servlet-api.jar under the Tomat installation directory Common\lib to Classpath, and modify the Classpath as follows:

Classpath=%java_home%\lib\dt.jar;
%java_home%\lib\tools.jar;
C:\Program Files\apache Software Foundation\tomcat 5.5\common\lib\servlet-api.jar;


starts Tomcat, accesses http://localhost:8080 in IE, and installs successfully if you see the Tomcat welcome page.

install MySQL

Extract mysql-4.1.13-win32.zip, run setup.exe

First appear is the Installation Wizard welcome interface, click "Next" directly Continue, select the installation type, select Custom Custom installation, and then click Next to install the custom installation interface, select the installation path: C:\MySQL Server 4.1 (customizable) point "OK" back to the Custom installation interface, path changed to set path, dot " Next ", ready to start installation, click" Install "to start the installation, after the completion of the creation of the MySQL.com account interface.

If you are using MySQL for the first time, select "Create anew free mysql.com accout", click "Next", enter your email address and set your own password for login to mysql.com, and then click "Next" when you finish filling in. Enter the second step, fill in the name and other related information, fill out the point "next", enter the third step, fill in the phone number, company name and other information, point "next", and then appear preview you just fill in the information of the interface, click "Next" appears installation completion interface.

Note that there is a configuration Wizard option (Configure the MySQL Server now), and it is recommended that you configure your MySQL immediately. Many say the installation of MySQL can not start after the reason is not configured MySQL.

Click Finish to complete the installation and start configuring MySQL, click "Next" and go to the Configuration Type Selection page. Select "Detailed Configuration" (Detailed configuration), click "Next", and enter the service type Selection page. Choose "Developer Machine" (Developer Machine), so that the system does not occupy a lot of resources, click "Next", enter the Database Usage Selection page.

Select "Multifunctional Database", click "Next", go to the Select InnoDB data storage location page without changing the settings, put it directly in the installation path installation directory, and then click "Next", Select the number of concurrent joins for MySQL, select "Manual Setting", set to 100 (set as appropriate, as required)

Click "Next" to configure MySQL's port in the TCP/IP communication environment to select the default 3306 port. Click "Next" to choose the character settings in MySQL, and note that the choices here will affect whether you can use Chinese in MySQL. Select the gb2312 character set to support Simplified Chinese, point "Next", set Windows service options, and note that the choice here is critical.

"Install as Windows Service" must be checked, this is the MySQL as Windows services run. "Service Name" is used under the default "MySQL" under "Launch the MySQL Server automatically" must be checked, so that when Windows starts, MySQL will automatically start the service, or you will manually start MySQL.

Many people say that after installing MySQL, unable to start, cannot connect, there are 10061 errors, the reason is here. Point "Next", set the root account root login password, "Modify security Settings" is to set the root account password, enter the password you set.

"Create a Anonymous account" is the creation of an anonymous accounts, which will cause unauthorized users to access your database illegally, there are security concerns, the proposal does not check.

Point "Next", the MySQL Configuration wizard will be based on all the settings above you to configure MySQL, so that the operation of MySQL to meet your needs, point "Execute" Start configuration, when the "Service started successfully", the description of your configuration completed, MySQL Service started successfully

Point "Finish" completed, the entire MySQL configuration completed, the rest is to use MySQL client connection MySQL server, and then used.

To install the JDBC driver:

Decompression Mysql-connector-java-3.1.10.zip

is going to be using Mysql-connector-java-3.1.10-bin-g.jar

and Mysql-connector-java-3.1.10-bin.jar

   Configuration

Mysqlforjdbc subdirectories are created in the C:\Program Files\java directory, and access to the directory will be mysql-connector-java-3.

1.10-bin.jar to this directory

Enter C:\Program

Files\java\jdk1.5.0_04\lib directory to copy Mysql-connector-java-3.1.10-bin-g.jar to this directory

Then configure Classpath to append%java_home%\lib\mysql-connector-java-3.1.10-bin-g.

Jar C:\Program

Files\java\mysqlforjdbc\mysql-connector-java-3.1.10-bin.jar to the environment variable.

After appending the environment variables are as follows:

Classpath=%java_home%\lib\dt.jar;
%java_home%\lib\tools.jar;
C:\Program Files\apache Software
Foundation\tomcat5.5\common\lib\servlet-api.jar;
%java_home%\lib\mysql-connector-java-3.1.10-bin-g.jar;
C:\Program FILES\JAVA\MYSQLFORJDBC
\mysql-connector-java-3.1.10-bin.jar;


The purpose of this configuration is to have the Java application find the driver to connect to MySQL.

  view and start the MySQL service

After installing MySQL under Windows XP, it has started the service automatically and has a shortcut connection to its client in the Start menu, which can be viewed through Windows Service Manager. "Start"-"Run", enter "Services.msc", carriage return.

Windows Service Manager pops up, and then you can see the service item with the service name "MySQL", which is labeled "Started" on the Start menu-All Programs-mysql-mysql Server 4.1-mysql Command Line The client uses the shortcut connection of the clients to enter the password set at installation.

  use of the database

After MySQL is installed, in the Start menu-All Programs-mysql-mysql Server 4.1-mysql Command line client uses the client's shortcut connection

Enter the password set at installation

Basic commands for MySQL (you must have a semicolon at the end of the MySQL command line edit after each input command)

Display database: show databases;

Using database: Use database name;

  Build a library

Build a database in MySQL, and a table in the database about

Command: Create database A;

Set permissions for a database (user and password)

Command:

Grant all privileges on first.* to Test@localhost identified by "123456";


When you're done with this command, you can only do this with the user name: Test, Password: 123456 When you log on, so that you avoid using the root

Enter command: Use A;

Use of the A-database;

To build a table in the I-Library

Command:

CREATE table about (ID int (8)
Primary key,name varchar (10));


In the table if the data:

Command:

Insert into about values
(' xyw1026 ', ' Laojiang ');


Exit

Command:

Exit


JSP connection MySQL

C:\Program files\apache Software Foundation\tomcat5.5\webapps directory to create subdirectories MyApp

Enter the C:\Program files\apache Software Foundation\tomcat5.5\webapps\myapp Directory

Write a file in Notepad to save as first.jsp

The code is as follows:

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.sql.*"%>
<body>
<%class.forname
("Org.gjt.mm.mysql.Driver"). newinstance ();
String url = "Jdbc:mysql:
Localhost/softforum?user=
Soft&password=soft1234&useunicode=
True&characterencoding=8859_1 "
The name of your database
Connection conn= drivermanager.getconnection (URL);
Statement stmt=conn.createstatement
(Resultset.type_scroll_sensitive,
resultset.concur_updatable);
String sql= "select * from";
ResultSet rs=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>


In the browser, enter:

  http://127.0.0.1:8080/myapp/first.jsp


If present:

Id|num
0 |laojiang


Database operation successful, congratulations to you, indicates a successful configuration

Enter C:\Program files\apache Software Foundation\tomcat5.5\webapps\myapp Directory

Create a new directory in the MyApp directory Web-inf, note that the directory name is case-sensitive;

Under Web-inf, create a new file with Notepad named Web.xml, which reads as follows:

<?xml version= "1.0" encoding= "Iso-8859-1"?>
<! DOCTYPE Web-app
Public "-//sun Microsystems,
INC.//DTD Web Application 2.3//en "
"Http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>my Web application</display-name>
<description>
A Application for test.
</description>
</web-app>


Under MyApp, create a new test JSP page with Notepad, the file name is index.jsp, and the file reads as follows:

Now: <%=new java.util.Date ()%>
</center></body>


  Restart Tomcat

Open the browser and enter http://localhost:8080/myapp/index.jsp

When you see the current time, the installation is successful.

To build your own servlet:

Create a new servlet program with Notepad, the file name is Helloworld.java, and the file reads as follows:

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


  compiling

Cut the Helloworld.java to the C:\Program files\apache Software foundation\tomcat5.5\common\classes Directory
Command-Line Input:

CD C:\Program Files\apache Software Foundation
\tomcat5.5\common\classes
Javac Helloworld.java

The


compiles this file with Javac Helloworld.java, and C:\Program files\apache Software
If an import javax.servlet.*

occurs.
Copy of the Servlet-api.jar file in the Foundation\tomcat5.5\common\lib directory and add to

C:\Program files\java\jre1.5.0_04\lib\ Ext directory to go to it.

then produces a compiled servlet file under the C:\Program files\apache Software

foundation\tomcat5.5\common\classes:

Helloworld.class

Use Notepad to open C:\Program files\apache Software

foundation\tomcat5.5\webapps\root\ The web.xml in the Web-inf directory is shown below, and the

content is added as follows:

<!--jspc servlet mappings Start-->
<servlet>
<servlet-name>org.apache.
Jsp.index_jsp</servlet-name>
<servlet-class>org.apache.
Jsp.index_jsp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>org.apache.
Jsp.index_jsp</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>

<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>
<!--jspc servlet mappings End-->
</web-app>


The servlet section of this passage declares the servlet you want to invoke, while servlet-mapping "maps" the declared servlet to the/servlet/helloworld address.

Start Tomcat, start the browser, enter Http://localhost:8080//servlet/HelloWorld, and if you see output helloworld! The servlet that wrote the instructions was successful. Note: If you have modified the Web.xml and added class, restart Tomcat and you can test it in the directory you created:

Copy Helloworld.class to C:\Program Files\apache Software

The Foundation\tomcat5.5\webapps\myapp\web-inf\classes directory

Where the classes directory does not create a

Use Notepad to open the Web.xml in the C:\Program files\apache Software foundation\tomcat5.5\webapps\myapp\web-inf directory, as shown below, with the following modifications:

<!--jspc servlet mappings Start-->

<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>
<!--jspc servlet mappings End-->
</web-app>


Start Tomcat, start the browser, enter http://localhost:8080/myapp/servlet/HelloWorld If you see the output helloworld! The servlet that wrote the instructions was successful. Note: If you have modified Web.xml and added class, restart Tomcat and build your own JavaBean:

Create a new Java program with Notepad, the file name is Testbean.java, and the file reads as follows:

Package test;
public class Testbean
{
Private String name = NULL;
Public Testbean (String strname_p)
{
this.name=strname_p;
}
public void SetName (String strname_p)
{
this.name=strname_p;
}
Public String GetName ()
{
return this.name;
}
}


  compiling

Place the Testbean.java under C:\Test and compile with the following command:

C:\test>javac Testbean.java


Then a compiled bean file is generated under C:\Test: Testbean.class

Cut the Testbean.class file to C:\Program Files\apache Software

In the Foundation\tomcat5.5\webapps\myapp\web-inf\classes\test directory,

If no subdirectories are created, create a new testbean.jsp file with the following file:

<%@ page import= "test. Testbean "%>
<%
Testbean testbean=new Testbean
("This is a test Java bean.");
%>
Java Bean name is:
<%=testbean.getname ()%>
</center></body>


Reboot Tomcat, start browser, enter http://localhost:8080/myapp/TestBean.jsp If you see output java Bean name Is:this is a test Java bean to explain The written JavaBean succeeded.



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.