java分层写一个关于jdbc和servlet简单的注册登录表单
在Java中,一般将应用程序分成多个层次,通常是三层或四层架构。在此建议采用三层架构:表示层(Servlet)、业务逻辑层和数据访问层(JDBC)。
先配置tomcat然后建项目
不会的可以看下面我之前发的博文或者在网上搜索
http://t.csdn.cn/HrV11
项目结构图
需要建的文件
数据库
# 建库
create database mydb ;
#建表
create table t_user
(
uid int primary key auto_increment,
username varchar(20),
password varchar(20),
phone varchar(11),
address varchar(50)
);
#插入数据
insert into t_user(username,password,phone,address) values('张三','666','18965423548','南阳');
insert into t_user(username,password,phone,address) values('李四','333','18754263548','许昌');
insert into t_user(username,password,phone,address) values('小美','123','18565234759','信阳');
create table t_goods
(
gid int primary key auto_increment,
gname varchar(20),
price double,
mark varchar(100)
);
insert into t_goods(gname,price,mark) values('泡面',4.5,'够香够辣就是这个味!');
insert into t_goods(gname,price,mark) values('火腿',8.5,'肉质细腻Q弹!');
insert into t_goods(gname,price,mark) values('雪碧',3.5,'清爽冰凉随心爽!');
页面设置
再把写页面写好
在web包里新建几个jsp文件
1.index.jsp文件
<%--
Created by IntelliJ IDEA.
User: yyz
Date: 20/2/2023
Time: 下午7:02
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>红浪漫登录页面</title>
</head>
<body>
<form action="index" method="post" >
账号:<input type="text" name="username" value=""/> <br/>
密码:<input type="password" name="password" value=""/> <br/>
<input type="submit" value="登录"> <a href="zhuce.jsp">注册</a>
</form>
</body>
</html>
2.zhuce.jsp文件
<%--
Created by IntelliJ IDEA.
User: yyz
Date: 20/2/2023
Time: 下午6:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="zhuce" method="post" >
账号:<input type="text" name="username" value=""/> <br/>
密码:<input type="password" name="password" value=""/> <br/>
手机号:<input type="text" name="phone" value=""/> <br/>
地址:<input type="text" name="address" value=""/> <br/>
<input type="submit" value=注册><a href="index.jsp">已有账号?登录</a>
</form>
</body>
</html>
3.zhuye.jsp文件
<%@ page import="java.util.List" %>
<%@ page import="com.yyz.bean.Goods" %><%--
Created by IntelliJ IDEA.
User: yyz
Date: 20/2/2023
Time: 下午6:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
</head>
<body>
<table>
<thead>
<tr>
<th>商品编号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品说明</th>
</tr>
</thead>
<tbody id="list">
<%
//获取session
HttpSession session1 = request.getSession();
List<Goods