昨天用java写了一个简单的web,实现了登录注册,工具是Eclipse,部署在Tomcat,用了一下jsp+servlet,前台页面用了一下bootstrap,数据库用的MySQL,虽然非常简单,但是还是出现了很多错误,主要问题是在前台向后台传输数据、连接数据库和写sql代码,连接数据库要用mysql-connector-java-5.1.7-bin.jar包。直接看代码吧:
界面:bootstrap的包和jquery-1.7.2要引正确,按ctr键,鼠标点击便可进入说明路径正确
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>网络交友</title> <link rel="stylesheet" type="text/css" href="./css/bootstrap.css"> <link rel="stylesheet" type="text/css" href="./css/bootstrap-theme.css"> <script type="text/javascript" src="./javascript/jquery-1.7.2.js"></script> <script type="text/javascript" src="./javascript/bootstrap.js"></script> <style type="text/css"> input.middle { vertical-align: middle; } label { vertical-align: middle; } </style> </head> <body> <div id="Layer" style="position: absolute; width: 100%; height: 100%; z-index: -1"> <img src="./images/shiChao.png" height="100%" width="100%" /> </div> <div style="width: 82px; height: 67px; background-image: url(./images/backGroundIcon.png); position: absolute; top: 11%; left: 53%"></div> <div style="font-size: 30px; position: absolute; top: 20%; left: 65%; color: #ffffff; font-family: 微软雅黑">网络交友</div> <form method="post" action="form"> <table style="position: absolute; top: 33%; left: 61%"> <tr> <td> <div class="input-group"> <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span> <input style="width: 261px; height: 40px" type="text" class="form-control" name="username" placeholder="用户名"> </div> <div style="height: 20px"></div> </td> </tr> <tr> <td> <div class="input-group"> <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span> <input style="width: 261px; height: 40px" type="password" class="form-control" name="password" placeholder="密码"> </div> <div style="height: 15px"></div> </td> </tr> <tr> <td> <div> <div style="float: left; font-size: 15; color: #000000"> <input id="checkbox" type="checkbox"><label style="font-weight: 400" for="checkbox"> 记住密码</label> </div> <div style="text-align: right"> <a href="ZhuCe.jsp" style="font-size: 15; color: #000000">注册</a> </div> <div style="height: 15px"></div> </div> </td> </tr> <tr> <td> <div> <button id="submit" style="width: 300px; height: 40px; background-color: #3162BB;" type="submit" class="btn btn-primary btn-lg">登 录</button> </div> </td> </tr> </table> </form> </body> </html>
在web.xml文件里可以设置启动页面,和页面对应的类servlet
注意:类的地址(包名.类名等)
代码:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>System01</display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>form</servlet-name> <servlet-class>org.system.login.FormServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>form</servlet-name> <url-pattern>/form</url-pattern> </servlet-mapping> </web-app>
处理数据的后台类FormServlet.class
package org.system.login; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FormServlet extends HttpServlet { private static final long serialVersionUID = 1L; public static Connection conn = null; /** * 重写service方法 处理前台传过来的数据和处理成功后跳转页面 */ @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置客户端的格式 request.setCharacterEncoding("utf-8"); // 获取Login.Jsp中控件输入的值 String name = request.getParameter("username"); String pwd = request.getParameter("password"); System.out.println("姓名:" + name); System.out.println("密码:" + pwd); // 驱动程序名 String driver = "com.mysql.jdbc.Driver"; // URL指向要访问的数据库名study String url = "jdbc:mysql://127.0.0.1:3306/study"; String user = "root"; // MySQL配置时的用户名 String password = "123456"; // MySQL配置时的密码 try { // 加载驱动程序 Class.forName(driver); // 连接数据库 Connection conn = DriverManager.getConnection(url, user, password); if (!conn.isClosed()) System.out.println("Succeeded connecting to the Database!"); // statement用来执行sql语句 Statement statement = conn.createStatement(); // 写sql一定要注意 String sql = "select * from user where username = " + "'" + name + "'"; System.out.println(sql); // sql执行的结果集 ResultSet rs = statement.executeQuery(sql); if (rs.next()) { String pwdStr = null; // 选择pwd这列数据 pwdStr = rs.getString("pwd"); System.out.println(pwdStr); if (rs.getString("pwd").equals(pwd)) { // 登录成功跳转的页面 response.sendRedirect("./loginSucess.jsp"); } } else { String sqlStr = "insert into user(username,pwd) values('" + name + "','" + pwd + "')"; Statement MyStatement; try { MyStatement = conn.createStatement(); MyStatement.executeUpdate(sqlStr); System.out.println("添加成功"); // 注册成功后跳转的页面 response.sendRedirect("./index.jsp"); } catch (Exception e) { System.out.println("添加失败"); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } }
虽然写的又简单又挫,但是还是有一定收获的,加油!
运行结果: bootstrap框架还是比较漂亮的