Java Getting started the fifth step of the database project actual combat "turn"

Source: Internet
Author: User
Tags tomcat server

Some tools are prepared before you actually go into code writing:

1. Ensure that there is a database available, here I use SQL Server 2000 for example, 2. Owning an IDE, such as Ecelise or myeclipse, I'm using MyEclipse 8.5

3. The database connection of the rack package;

The installation of the database here will not say, if you install SQL Server 2000, you can use JDBC to connect with the need to hit the SP3 patch;

When everything is ready, we start to get to the point:

1. First we---->web Project in file--->new (can also right-click in the package Explore view)

Enter the project name in Project name (here, enter HelloWorld, count as our first Web project), and click Finish; you can see a Web project on the right:

The role of each part of the above is not detailed here, you can consult the basic books to understand

2. Then we are going to put the jar of data connected to the Lib directory under Web-inf (we will put the required jar in this directory in the future):

Due to the IDE support responsible and paste, we only need to be responsible for the jar package after the IDE project under the Lib directory will be pasted.

3. We first in the page end index.jsp (or you can create a new JSP or HTML page) to write a simple registration page, the code is as follows:

Import= "java.util.*" pageencoding= "Utf-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

Note: We generally change the encoding format to Utf-8, <%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>

Next we look at the effect of the page, regardless of background interaction, here we need to load the project into the application server, here we choose the Open source server Tomcat 6.0,
Specific installation can be seen in: Java Getting started the fifth step of the installation of Tomcat http://www.cnblogs.com/shenliang123/archive/2012/04/24/2468036.html

4. Next we deploy the project:

First click on the left button to deploy the project

At Project Select the project you want to deploy, select HelloWorld here, then click the Add button, in the interface that appears, select the Tomcat server in the server, select Tomcat 6.x----> click Finish

Then click OK after successful loading, project deployment is over.

5. You can now start the server, after clicking on the right button after selecting Tomcat 6.x----->start in the dropdown box, you can see the console will print out the information----> after the start time to show that the load is complete

6. Now you can open the browser to access it; In the Open browser address, enter the address: http://localhost:8080/HelloWorld/index.jsp

You can see the output on the Web page: (Here our page of aesthetics first not to tube)

--------------------------------------------------------------------------------------------------------------- ------------------------------------------------------

Next we have to write the background code, in order to interact with the data, we can see the new page above our background processing is in the Registerservlet;

In code writing, we first create a new test database in SQL Server 2000, with fields of Id,username,password where ID is the self-increment

1. We now create a new servlet in the SRC directory of the current project: Select the SRC directory right-click---->new----->servlet----->

The name option must be populated with the servlet name that the page side needs to access----->next----> Click finish by default---->

Now we can go to the Web-inf under the Web. XML: There is the following code

  <servlet>    <description>this is the description of my EE component</description>    < Display-name>this is the display name of my EE component</display-name>    <servlet-name> registerservlet</servlet-name>    <servlet-class>xidian.sl.servlet.registerservlet</ servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>registerservlet </servlet-name>    <url-pattern>/servlet/RegisterServlet</url-pattern>  </ Servlet-mapping> 

Simple principle: page-side access to the servlet-----> in the Web. XML configuration file <servlet-mapping> tags under the <servlet-name> tag to find the corresponding name-----> When found, it will be based

<url-pattern> tag under the name go to <servlet> tab under <servlet-name> look for the name corresponding to the------> found after the <servlet-class> The full name below to find the specific location of the servlet;

After reading the above meanings, we should know that we do not have to choose new----->servlet when creating a new servlet class, or we can choose the new----->classes directly, and then configure it manually in the Web. Xml.

The former practice is relatively lazy practice, we can try the latter look;

2. Next we go back to our new servlet class, and we can see that the IDE tools have generated some of the code for us, and the most important thing in the servlet is the Doget () and Dopost () two methods that call which

method, look at the page end of the submission method: by method= "POST" can be submitted in the form of post, so our code can be written only in Dopost,

Before writing the code, we are going to connect the database, and here I am using my own packaged class: Http://www.cnblogs.com/shenliang123/archive/2012/04/19/2456665.html

For the sake of convenience I put the class directly under the same package as the servlet and can be placed separately when you do it yourself:

The Dopost method code in the Servlet class:

PublicvoidDoPost (HttpServletRequest request, httpservletresponse response)ThrowsServletexception, IOException {response.setcontenttype ("text/html"); Response.setcharacterencoding ("Utf-8"); Request.setcharacterencoding ("Utf-8");/*** Use Request.getparameter (String str) to get the parameters of the page-side pass, the parameter of the method is the name value of the page-side pass **/String username = request.getparameter ("username");//Account String Password = request.getparameter ("password");//Password//Print to see if the value has been obtained SYSTEM.OUT.PRINTLN ("Username:" +username + "Password:" +password); PrintWriter out =Response.getwriter ();Try{/*** The following is based on the account to query the database to see if the user name has been applied **/String sql = "SELECT id from Users where username= '" +username+ "'"; Dbconn dbconn =NewDbconn (); string[][] data =Dbconn.getdata (SQL);if (data = =null| | (Data! = null&& data.length = = 0// add a database of String Sql_insert = "INSERT into users (username, password) VALUES ('" + Username+ "', '" +password+ "')" ); Out.flush (); Out.close (); }else{out.println ("<script language= ' JavaScript ' >alert (' account has been applied, please re-lose! window.location.href= ' index.jsp ';</script> "); }}catch (Exception e) {e.printstacktrace ();}}   

Then visit the page end, enter the user name and account after clicking Register, return to success:

To view the database:

To this our first Web project is declared successful, at this stage we do not have much control over the page, in the future learning will slowly join

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.