昨天用java写了一个简单的web,实现了登录注册,工具是Eclipse,部署在Tomcat,用了一下jsp+servlet,前台页面用了一下bootstrap,数据库用的MySQL,虽然非常简单,但是还是出现了很多错误,主要问题是在前台向后台传输数据、连接数据库和写sql代码,连接数据库要用mysql-connector-java-5.1.7-bin.jar包。直接看代码吧:
界面:bootstrap的包和jquery-1.7.2要引正确,按ctr键,鼠标点击便可进入说明路径正确
pageEncoding="utf-8"%>
html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
网络交友input.middle {
vertical-align: middle;
}
label {
vertical-align: middle;
}
style="position: absolute; width: 100%; height: 100%; z-index: -1">
style="width: 82px; height: 67px; background-p_w_picpath: url(./p_w_picpaths/backGroundIcon.png); position: absolute; top: 11%; left: 53%">
style="font-size: 30px; position: absolute; top: 20%; left: 65%; color: #ffffff; font-family: 微软雅黑">网络交友
class="glyphicon glyphicon-user"> style="width: 261px; height: 40px" type="text" class="form-control" name="username" placeholder="用户名"> |
class="glyphicon glyphicon-lock"> style="width: 261px; height: 40px" type="password" class="form-control" name="password" placeholder="密码"> |
style="font-weight: 400" for="checkbox"> 记住密码 注册 |
style="width: 300px; height: 40px; background-color: #3162BB;" type="submit" class="btn btn-primary btn-lg">登 录 |
在web.xml文件里可以设置启动页面,和页面对应的类servlet
注意:类的地址(包名.类名等)
代码:
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">
System01
login.jsp
form
org.system.login.FormServlet
form
/form
处理数据的后台类FormServlet.classpackage 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框架还是比较漂亮的