Simple Java Web application setup and deployment

Source: Internet
Author: User
Tags java web server port
1. Preparation work

Tools: Tomcat, EditPlus, JDK, Windows operating system

Action: Install JDK, Tomcat, EditPlus on the Windows operating system, configure Java_home,path,classpath, and add Servlet-api.jar to the CLASSPATH path

CLASSPATH D:\Program Files\java\jdk1.6.0_10\lib; F:\tomcat\lib\servlet-api.jar;

You can also configure Catalina_home F:\tomcat


2, Tomcat, Servlet introduction

Tomcat is a completely free web server

The servlet container, also called the servlet engine, is part of a Web server or application server that provides network services on top of sent requests and responses, decodes a MIME based request, and formats a MIME based response


After extracting Tomcat, get the following directory, of course TOMCAT7 only a Lib directory. The Servlet-api.jar to be used is placed in the Lib directory

Servlet life cycle

3. Java Web application

A Web application is a complete application of a set of Servlet, class, HTML pages, and other resources running on a Web server. A context represents an application

Take a landing servlet as an example to introduce the development process of the Java Web program, complete the function: User login through login.html, login successfully return some information of client and server, and request information

4, directory structure

F:\tomcat\webapps\servlet_demo This is the path to the application Servlet_demo on my computer, the path is the context path, where the following files are created:

5, the location of each file and source code

Lib directory to store the resources we need to store here is the MySQL JDBC driver

The SRC directory is decentralized source code, which can be deployed without the need for

The DatabaseConnection class is a common database connection tool class

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.SQLException;

Import java.sql.Statement; /** * The tool class is used for database connections, and returns a statement object from which the user can execute SQL statements to obtain results/public class DatabaseConnection {public static final String
	Access_driver = "Sun.jdbc.odbc.JdbcOdbcDriver";
	public static final String mysql_driver = "Com.mysql.jdbc.Driver";
	public static final String oracle_driver = "Oracle.jdbc.driver.OracleDriver";

	public static final String sqlserver_driver = "Com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private Statement st = null;

	Private Connection Connection = null; Public Statement getstatement (string driver, string URL, string user, string password) {try {//1, loading driver clas

			S.forname (driver);

			2, through the URL to establish a connection to the database connection = drivermanager.getconnection (URL, user, password);

		3, create the statement, connection can see the cable road, statement can see the cable car st = Connection.createstatement ();
		catch (ClassNotFoundException e) {e.printstacktrace (); catch (SqlexcEption e) {e.printstacktrace ();
	Return St;
			public void Close () {try {if (St!= null) st.close ();
		if (connection!= null) connection.close ();
		catch (SQLException e) {e.printstacktrace ();
 }
	}
}


The Loginservlet class is used to process form data, to determine if the username password is correct, to return information if it is correct, and to go to the login interface

Import Javax.servlet.RequestDispatcher;
Import Javax.servlet.ServletContext;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;
Import java.io.*;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;

Import java.util.Enumeration; public class Loginservlet extends HttpServlet {protected void doget (HttpServletRequest req, HttpServletResponse resp) th
	Rows Servletexception, IOException {doPost (req, resp); } protected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {//Set
		Response MIME type resp.setcontenttype ("Text/html;charset=utf-8");
		Get output stream PrintWriter out = Resp.getwriter ();
			if (Isright (req, resp)) {//Get client information and server information String clientaddress = Req.getremoteaddr ();
			int clientport = Req.getremoteport ();
			String clienthost = Req.getremotehost (); String serveraddress = rEq.getlocaladdr ();
			int serverport = Req.getlocalport ();

			String ServerHost = Req.getlocalname ();
			Output information to the customer browser out.print (" 


Landing Page login.html

 

Database Information


6, Operation

After the 5th step is done, open Tomcat and database, enter in the Address bar: Http://localhost:8080/servlet_demo will enter the login interface.

Of course, you can also run it by Telnet:

Connect First:
telnet localhost 8080
Then visit:
Get/servlet_demo/login http/1.1
Host:localhost

Double-click the return row

7, packaging into a war file release

DOS window switch to F:\tomcat\webapps\servlet_demo. Execute command: JAR-CVF Servlet_demo.war *. There is a Servlet_demo.war file in the current directory, you can open the file directly with the compression software, or you can use the command: JAR-TF servlet_demo.war to open the file.

Delete the WebApps directory to Servlet_demo application, put Servlet_demo.war files into the directory, so that the deployment is good, run the same as the 6th step ...

8, about the difference in the servlet redirect

9. Reference materials

"Java Web Development detailed"

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.