javaweb小项目练习jsp操作
操作只有两个java包
该系统主要由图书订购系统框架页面、图书订购系统主页、购物车页面和注册页面组成,
1.图书订购系统框架页面,并显示商品的一部分frame.jsp文件如下:
<%--
Created by IntelliJ IDEA.
User: 徐大大
Date: 2022-01-09
Time: 10:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>图书订购系统</title>
</head>
<%--
整体的框架的描写
--%>
<frameset id="frame" frameborder="0" rows="100,*" border="false" scrolling="yes">
<frame name="topframe" scrolling="auto" marginheight="0" marginwidth="0" src="header.jsp" noresize>
<frameset framespacing="0" border="false" cols="200,*" frameborder="0" scrolling="yes">
<frame name="leftFrame" scrolling="no" marginheight="0" marginwidth="0" src="usingpage.jsp" noresize>
<frame name="rightFrame" scrolling="auto" src="rightFrame.jsp" marginwidth="0" marginheight="0">
</frameset>
</frameset>
<body>
</body>
</html>
2.页头页面 如header.jsp
<%--
Created by IntelliJ IDEA.
User: 徐大大
Date: 2022-01-09
Time: 10:50
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title></title>
</head>
<body topmargin="0" leftmargin="0" rightmargin="0">
<form action="">
<table width="100%" height="79" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td bgcolor="F9A859" valign="top">
<table width="100%" height="50" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="FBEAD0">
<tr>
<td align="center" style="font-size: 22px">
图书订购系统
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor="F9A859" valign="top">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" style="font-size: 13px" valign="middle">
欢迎您的访问!
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
3.用户登录模块usingpage.jsp
<%--
Created by IntelliJ IDEA.
User: 徐大大
Date: 2022-01-09
Time: 11:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
<%--用javaScript编写函数,在按钮触发该函数--%>
<script language="JavaScript">
function login() {
document.myform.operator.value="login";
document.myform.submit();
}
</script>
</head>
<body>
<h2>登录页面</h2>
<br>
<form action="show.jsp" name="myform" method="post">
用户名:<input type="text" name="name" size="10"> <br>
密 码:<input type="password" name="number" size="10"> <br><br>
<input type="hidden" name="operator" value="">
<a onclick="login()"><img src="login.gif" alt="登录"></a>
</form>
</body>
</html>
系统主页rightFrame.jsp
<%@ page import="com.shop.jb.DBBean" %><%--
Created by IntelliJ IDEA.
User: 徐大大
Date: 2022-01-09
Time: 11:10
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<table border="1">
<tr>
<th width="30%" align="center">书名</th>
<th width="30%" align="center">定价</th>
<th width="30%" align="center">作者</th>
<th width="30%" align="center">求购</th>
</tr>
<%
String cp =request.getParameter("cp");
int currpage = (cp == null || cp == "") ? 1 : Integer.parseInt(cp);
String[][] ss = DBBean.getGoodList(currpage);
for (int i = 0; i < ss.length; i++) {
%>
<tr>
<%--实现图书表单--%>
<td height="30%" align="center"> <%=ss[i][0]%> </td>
<td height="30%" align="center"> <%=ss[i][1]%> </td>
<td height="30%" align="center"> <%=ss[i][2]%> </td>
<td height="30%" align="center"> <a href="Buy.jsp?sid=<%=ss[i][0]%>&action=buy">购买</a> </td>
</tr>
<%
}
%>
</table>
<%
int tp = DBBean.getTotalPage();
if(currpage!=1){
%>
<a href="rightFrame.jsp?cp=<%=currpage-1%>">《《上一页</a>
<% }
if(currpage != tp) { %>
<a href="rightFrame.jsp?cp=<%=currpage+1%>">下一页》》</a>
<%
}
%>
<form action="rightFrame.jsp" name="myform">
<select name="top" onchange="document.myform.submit()">
<%
for (int i = 1; i < tp; i++) {
%>
<option value="<%=i%>"<%=(i==currpage)?"selected":""%>>第<%=i%>页</option>
<%
}
%>
</select>
</form>
<hr/>
<a target="_blank" href="Cart.jsp">查看购物车</a>
<%--跳转到Cart.jsp页面--%>
<a target="_blank" href="reg.jsp">用户注册</a>
</body>
</html>
5.购物车模块 Cart.jsp
<%@ page import="com.shop.jb.Cart" %>
<%--
Created by IntelliJ IDEA.
User:徐大大
Date: 2022-01-09
Time: 11:52
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="cart" scope="session" class="com.shop.jb.Cart"/>
<html>
<head>
<title>购物车</title>
</head>
<body>
<%
if (cart.isEmpty()){
%>
<font color="red" size="20">购物车中没有任何商品!!</font>
<%
} else {
%>
<h2>购物车</h2>
<table border="1">
<tr>
<td width="27%" align="center">书名</td>
<td width="27%" align="center">定价</td>
<td width="27%" align="center">作者</td>
<td width="27%" align="center">数量</td>
<td width="27%" align="center">求购</td>
</tr>
<%
String[][] ssa = cart.getCart();
for (int i = 0; i < ssa.length; i++) {
%>
<tr>
<%--实现购物车表单--%>
<th height="30%" align="center"><%=ssa[i][0]%></th>
<th height="30%" align="center"><%=ssa[i][1]%></th>
<th height="30%" align="center"><%=ssa[i][2]%></th>
<th>
<form action="Buy.jsp" method="post">
<input type="text" name="count" value="<%=ssa[i][3]%>">
<input type="hidden" name="sid" value="<%=ssa[i][0]%>">
<input type="hidden" name="action" value="gc">
</form>
</th>
<th><a href="Buy.jsp?sid=<%=ssa[i][0]%>&action=del">删除</a></th>
</tr>
<%
}
%>
</table>
<br>
<br>
本订单总价为: <%=Math.round(cart.getTotal()*100)/100.0%>
<%
}
%>
</td>
<br>
<hr/>
<a target="_blank" href="frame.jsp">继续购物</a>
</body>
</html>
6.对购物车进行操作Buy.jsp
<%--
Created by IntelliJ IDEA.
User: 徐大大
Date: 2022-01-09
Time: 12:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="cart" scope="session" class="com.shop.jb.Cart"/>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
String action = request.getParameter("action");
if(action == null){
%>
<jsp:forward page="rightFrame.jsp"/>
<%
} else if (action.trim().equals("buy"))
{
String sid = request.getParameter("sid");
cart.add(sid.trim());
%>
<jsp:forward page="rightFrame.jsp"/>
<%
}
else if (action.trim().equals("gc")){
String sid = request.getParameter("sid");
String count = request.getParameter("count");
cart.setCount(Integer.parseInt(count),sid);
%>
<jsp:forward page="Cart.jsp"/>
<%
}
else if(action.trim().equals("del"))
{
String sid = request.getParameter("sid");
cart.deleteFromCart(sid);
%>
<jsp:forward page="Cart.jsp"/>
<%
}
%>
</body>
</html>
7.注册模块 show.jsp
<%--
Created by IntelliJ IDEA.
User: 徐大大
Date: 2022-01-09
Time: 12:19
To change this template use File | Settings | File Templates.
--%>
<%@page contentType="text/html; ISO-8859-1" import="java.sql.*" errorPage="error.jsp" %>
<html>
<head>
<title>Title</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
<%
String operator = request.getParameter("operator");
if ("login".equals(operator))
{
%>
<jsp:forward page="load.jsp"/>
<%--网页重定向--%>
<%
}
%>
</body>
</html>
8.注册对接数据库load.jsp
<%--
Created by IntelliJ IDEA.
User: 徐大大
Date: 2022-01-09
Time: 12:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" import="java.sql.*" language="java" %>
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html" charset="GB2312">
</head>
<body>
<%
String name = (String)request.getParameter("name");
String password = (String)request.getParameter("number");
Connection connection=null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc.mysql://localhost:3306/shop?useSSL=true&characterEncoding=utf-8&user=root&password=");
PreparedStatement preparedStatement = connection.prepareStatement("select * from useinfo where name =? number =?");
preparedStatement.setString(1, name);
preparedStatement.setString(2, password);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
session.setAttribute("name", name);
response.sendRedirect("Dreg4.jsp");
} else {
response.sendRedirect("error1.jsp");
}
}
catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>
9.注册接口进入模块reg.jsp
<%--
Created by IntelliJ IDEA.
User: 徐大大
Date: 2022-01-09
Time: 12:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.sql.*" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<textarea name="textfield" cols="60" rows="20">
这是一个图书顶购系统
</textarea>
<table>
<tr>
<td>
<form action="reg1.jsp" method="post">
<input type="button" name="同意" value="同意" onclick="submit()">
</form>
</td>
<td>
<form action="frame.jsp" method="post">
<input type="button" name="不同意" value="不同意" onclick="submit()">
</form>
</td>
</tr>
</table>
</body>
</html>
10.对注册信息进行操作页面reg1.jsp
<%--
Created by IntelliJ IDEA.
User: 徐大大
Date: 2022-01-09
Time: 12:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.sql.*" %>
<html>
<head>
<title></title>
</head>
<body>
<h2>注册</h2>
<br>
<form name="info" action="reg2.jsp"method="post">
用 户 名:<input type="text" name="name" value=""><br>
密 码:<input type="password" name="password" value=""><br>
确认密码:<input type="password1" value="" name="password1"> <br>
真实姓名:<input type="text" name="relname" value=""> <br>
电 话:<input type="text" name="telephe" value=""> <br>
E-mail:<input type="text" name="mail" value=""> <br>
住 址:<input type="text" name="adder" value=""> <br>
<input type="button" name="next" value="下一个" onclick="Checkform()">
</form>
<%--用checkform函数进行客户端验证--%>
<script language="JavaScript">
function Checkform() {
flag = 1;
if(document.info.password.value != document.info.password1.value)
{
alert("两次密码不相同");
document.info.password.value="";
document.info.password1.value="";
flag = 0;
}
if(document.info.name.value==""){
alert("请输入用户名");
document.info.name.focus();
flag = 0;
return false;
}
if (document.info.password.value==""){
alert("请输入密码");
document.info.password.focus();
flag=0;
return false;
}
if(document.info.password1.value==""){
alert("请输入确认密码");
document.info.password1.focus();
flag = 0;
return false;
}
if(document.info.telephe.value==""){
alert("请输入电话号码");
document.info.telephe.focus();
flag = 0;
return false;
}
if(document.info.mail.value==""){
alert("请输入E-mail");
document.info.mail.focus();
flag = 0;
return false;
}
if(flag == 1) {
document.info.submit();
}
}
</script>
</body>
</html>
11.注册进行验证数据库 reg2.jsp
<%--
Created by IntelliJ IDEA.
User: 徐大大
Date: 2022-01-09
Time: 12:59
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.sql.*" %>
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html" charset="GB2312">
</head>
<body>
<%
String name = (String)request.getParameter("name");
String password = (String)request.getParameter("password");
String telephe = (String)request.getParameter("telephe");
String mail = (String)request.getParameter("mail");
String relname = (String)request.getParameter("relname");
String adder = (String)request.getParameter("adder");
try{
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop?useSSL=true&characterEncoding=utf-8&user=root&password=");
PreparedStatement preparedStatement = connection.prepareStatement("insert into useinfo values(?,?,?,?,?,?)");
preparedStatement.setString(1, name);
preparedStatement.setString(2, password);
preparedStatement.setString(3, relname);
preparedStatement.setString(4, telephe);
preparedStatement.setString(5, mail);
preparedStatement.setString(6, adder);
int count = preparedStatement.executeUpdate();
if(count > 0) {
%>
<%--网页重定向--%>
<jsp:forward page="reg3.jsp"/>
<%
} else {
%>
<jsp:forward page="frame.jsp"/>
<%
} connection.close();
} catch(Exception e) {
e.printStackTrace();
%>
<jsp:forward page="error.jsp"/>
<%
} finally {
}
%>
</body>
</html>
12.注册成功提示页面reg3.jsp
<%--
Created by IntelliJ IDEA.
User: 徐大大
Date: 2022-01-09
Time: 13:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.sql.*" %>
<html>
<head>
<title></title>
</head>
<body>
<h2>注册成功!</h2>
<a href="frame.jsp">返回</a>
</body>
</html>
13.错误信息提示页面 error.jsp/ error1.jsp
<%--
Created by IntelliJ IDEA.
User: 徐大大
Date: 2022-01-09
Time: 13:14
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" import="java.sql.*" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>该用户已经存在</h2>
<a href="frame.jsp">返回</a>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: 徐大大
Date: 2022-01-09
Time: 13:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.sql.*" %>
<html>
<head>
<title></title>
</head>
<body>
<h2>用户名密码出错</h2>
<h2>请重新登录</h2>
<a href="index.jsp">返回</a>
</body>
</html>
14.java代码实现商品的一些操作Cart.java
package com.shop.jb;
import java.sql.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class Cart {
HashMap hm = new HashMap();
double total = 0;
public void add(String sid) {
if (hm.containsKey(sid)) {
int xc = ((Integer)hm.get(sid)).intValue()+1;
hm.put(sid,new Integer(xc));
} else{
hm.put(sid,new Integer(1));
}
total = total+DBBean.getPrice(sid);
}
public double getTotal() {
return total;
}
public void setCount(int c,String sid) {
int yc = ((Integer)hm.get(sid)).intValue();
total = total + (c-yc)*DBBean.getPrice(sid);
hm.put(sid,new Integer(c));
}
public void deleteFromCart(String sid) {
int yc = ((Integer)hm.get(sid)).intValue();
total = total-yc*DBBean.getPrice(sid);
hm.remove(sid);
}
public boolean isEmpty() {
return hm.isEmpty();
}
public String [][] getCart() {
Set ks = hm.keySet();
Iterator ii = ks.iterator();
int size = hm.size();
String rs [][] = new String[size][];
for (int i = 0; i < size; i++) {
String sid = (String)ii.next();
String[] sa = new String[4];
sa[0]=sid;
String[] sat = DBBean.getDetail(sid);
sa[1]=sat[0];
sa[2]=sat[1];
sa[3]=((Integer)hm.get(sid)).toString();
rs[i]=sa;
}
return rs;
}
}
15.对数据库的全部操作DBBean.java
package com.shop.jb;
import javax.xml.transform.Result;
import java.sql.*;
import java.util.Vector;
public class DBBean {
static Connection connection;
static PreparedStatement psGoodList;
static PreparedStatement psGoodFromldToPrice;
static PreparedStatement psGoodFromldToDetail;
static PreparedStatement psCountRecords;
static int span = 5;
static {
try{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop?useSSL=true&characterEncoding=utf-8&user=root&password=");
psGoodList = connection.prepareStatement("select sid,sname,sprice from good", ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
psGoodFromldToPrice = connection.prepareStatement("select sprice from good where sid = ?");
psGoodFromldToDetail = connection.prepareStatement("select sname,sprice from good where sid=?");
psCountRecords = connection.prepareStatement("select count(sid) from good");
} catch (Exception e) {
e.printStackTrace();
}
}
public static String[][] getGoodList(int cp) {
String [][] result = null;
Vector v = new Vector();
try {
ResultSet rs = psGoodList.executeQuery();
if (cp != 1)
{
rs.absolute((cp-1)*span);
}
int count = 1;
while(rs.next() &&count <= span) {
count++;
String[] tempsa = new String[3];
tempsa[0]= rs.getString(1);
tempsa[1]= rs.getString(2);
tempsa[2]= rs.getString(3);
v.add(tempsa);
}
int size = v.size();
result = new String[size][];
for (int i = 0; i < size; i++) {
result[i] = (String[])v.elementAt(i);
}
} catch ( Exception e) {
e.printStackTrace();
}
return result;
}
public static double getPrice(String sid) {
double dd = 0;
try {
psGoodFromldToPrice.setString(1,sid);
ResultSet rs = psGoodFromldToPrice.executeQuery();
rs.next();
dd = Double.parseDouble(rs.getString(1));
} catch ( Exception e) {
e.printStackTrace();
}
return dd;
}
public static String[] getDetail(String sid) {
String[] ss = null;
try {
psGoodFromldToDetail.setString(1,sid);
ResultSet rs = psGoodFromldToDetail.executeQuery();
rs.next();
ss = new String[2];
ss[0] = rs.getString(1);
ss[1]= rs.getString(2);
}catch (Exception e) {
e.printStackTrace();
}
return ss;
}
public static int getTotalPage(){
int tp = 0;
try {
ResultSet rs = psCountRecords.executeQuery();
rs.next();
int count = rs.getInt(1);
tp = count/span+((count%span==0)?0:1);
}catch (Exception e) {
e.printStackTrace();
}
return tp;
}
public static void main(String args[])
{
System.out.println(DBBean.getTotalPage());
}
}
全部为jsp的主要操作,下面为sql数据库表的创建
;
;
;
;
;
;
CREATE DATABASE `shop` ;
USE `shop`;
DROP TABLE IF EXISTS `good`;
CREATE TABLE `good` (
`sid` int(11) NOT NULL,
`sname` varchar(20) NOT NULL,
`sprice` float NOT NULL,
`count` int(11) NOT NULL,
PRIMARY KEY (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into `good`(`sid`,`sname`,`sprice`,`count`) values (1,'java璇剧▼璁捐',36.5,10),(2,'sqlshujuku',39.2,20);
DROP TABLE IF EXISTS `useinfo`;
CREATE TABLE `useinfo` (
`name` varchar(20) NOT NULL,
`password` varchar(20) NOT NULL,
`relname` varchar(30) NOT NULL,
`telephe` varchar(30) NOT NULL,
`mail` varchar(30) NOT NULL,
`adder` varchar(60) NOT NULL,
PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into `useinfo`(`name`,`password`,`relname`,`telephe`,`mail`,`adder`) values ('r','r','r','r','r','r'),('root','root','xu','12','12','12');
;
;
;
;
操作演示
具体在此完整项目下载
还有什么问题可以留言给我,会尽心解答的,觉得文章不错的话点个赞把