web项目开发实例

  我的第一个web项目就是一个登陆的页面。主要的就是搭建服务器,配置Tomcat环境和eclipse开发。基本的源代码如下

login.jsp

 1 <%@ page import="java.sql.*" language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>登录界面title>
 8 head>
 9 <body>
10     <center>
11         <h1 style="color:red">登录h1>
12             <form id="indexform" name="indexForm" action="logincheck.jsp" method="post">
13                 <table border="0">
14                     <tr>
15                         <td>账号:td>
16                         <td><input type="text" name="username">td>
17                     tr>
18                     <tr>
19                         <td>密码:td>
20                         <td><input type="password" name="password">
21                         td>
22                     tr>
23                 table>
24             <br>
25                 <input type="submit" value="登录" style="color:#BC8F8F">
26             form>
27             <form action="zhuce.jsp">
28                 <input type="submit" value="注册" style="color:#BC8F8F">
29             form>
30     center>
31 body>
32 html>

logincheck.jsp

 1 <%@ page import="java.sql.*" language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 7 <title>Insert title heretitle>
 8 head>
 9 <body>
10 <jsp:useBean id="db" class="Bean.Bean" scope="page" />
11 <%
12     request.setCharacterEncoding("UTF-8");
13     String username=(String)request.getParameter("username");
14     String password=(String)request.getParameter("password");//取出login.jsp的值
15     
16     //下面是数据库操作
17     String sql="select * from login where username="+"'"+username+"'";//定义一个查询语句
18     ResultSet rs=db.executeQuery(sql);//运行上面的语句
19     if(rs.next())
20     {
21         /* if(password.equals(rs.getString(2)))
22         {
23             
24         } */
25         if(password.equals(rs.getObject("password"))){
26             response.sendRedirect("loginsuccess.jsp");
27         }
28         else{
29             out.print("");
30             response.setHeader("refresh", "0;url=login.jsp");
31         }
32     }
33     else 
34     {
35         out.print("");
36         response.setHeader("refresh", "0;url=login.jsp");
37     }
38     
39 %>
40 body>
41 html>

loginsuccess.jsp

 1 <%@ page import="java.sql.*" language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 7 <title>Insert title heretitle>
 8 head>
 9 <body>
10 <h1>登录成功 h1>
11 body>
12 html>

Bean.java

 1 package Bean;
 2 import java.sql.*;
 3 public class Bean {
 4     private String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
 5     private String connStr = "jdbc:sqlserver://localhost:1433; DatabaseName=JXP";
 6     private String dbusername = "sa";
 7     private String dbpassword = "密码";
 8     private Connection conn = null;
 9     private Statement stmt = null;
10 
11     public Bean()
12     {
13         try
14         {
15             Class.forName(driverStr);
16             conn = DriverManager.getConnection(connStr, dbusername, dbpassword);
17             stmt = conn.createStatement();
18         } 
19         catch (Exception ex) {
20             System.out.println("数据连接失败!");
21         } 
22         
23     }
24 
25     public int executeUpdate(String s) {
26         int result = 0;
27         System.out.println("--更新语句:"+s+"\n");
28         try {
29             result = stmt.executeUpdate(s);
30         } catch (Exception ex) {
31             System.out.println("执行更新错误!");
32         }
33         return result;
34     }
35 
36     public ResultSet executeQuery(String s) {
37         ResultSet rs = null;
38         System.out.print("--查询语句:"+s+"\n");
39         try {
40             rs = stmt.executeQuery(s);
41         } catch (Exception ex) {
42             System.out.println("ִ执行查询错误!");
43         }
44         return rs;
45     }
46     public void execQuery(String s){
47         try {
48             stmt.executeUpdate(s);
49         } catch (SQLException e) {
50             // TODO Auto-generated catch block
51             System.out.println("执行插入错误!");
52         }
53     }
54 
55     public void close() {
56         try {
57             stmt.close();
58             conn.close();
59         } catch (Exception e) {
60         }
61     }
62 }

这些简单的代码和数据库中的数据就可以构建一个简单的登陆界面。

  我觉得还是对Jsp和html的认识和使用不理解,对数据库的认识不深,这还需要我们的大量的练习。

转载于:https://www.cnblogs.com/wangfengbin/p/6440199.html

你可能感兴趣的:(web项目开发实例)