• web项目练习


    今天发现了前几天做的商品系统的一处bug,废了很大的力气才修改完成,在买商品的时候,不会去除库里的商品,今天吧bug 修复了,明天计划继续学习web技术。

    运行结果:

     

     

     

     

     

     

     

     

     

     

     代码部分:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>功能选择界面</title>
    </head>
    <body>
    <form>
    <table align="center" border="1">
    <tr align="center">
    <td>
    <input type="submit" value="增加商品" formaction="add.jsp" formmethod="post">
    </td>
    </tr>
    <tr align="center">
    <td>
    <input type="submit" value="删除商品" formaction="delete.jsp " formmethod="post">
    </td>
    </tr>

    <tr align="center">
    <td>
    <input type="submit" value="修改商品" formaction="update.jsp" formmethod="post">
    </td>
    </tr>
    <tr align="center">
    <td>
    <input type="submit" value="买商品" formaction="servlet?method=show" formmethod="post">
    </td>
    </tr>

    </table>
    </form>
    </body>
    </html>

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>增加商品页面</title>
    </head>
    <body>
    <script>
    var add='<%=request.getParameter("add")%>';
    if(add=="yes"){
    alert("添加成功!");
    }
    </script>
    <form action="servlet?method=add" method="post">
    <table align="center">

    <tr>
    <td>名称:</td>
    <td>
    <input type="text" name="name">
    </td>
    </tr>
    <tr>
    <td>价格:</td>
    <td>
    <input type="text" name="jiage">
    </td>
    </tr>
    <tr>
    <td>数量:</td>
    <td>
    <input type="text" name="shuliang">
    </td>
    </tr>
    <tr align="center">
    <td>
    <input type="submit" value="确定" >
    <input type="submit" value="返回" formaction="add.jsp" formmethod="post">
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>删除商品页面</title>
    </head>
    <body>
    <script>
    var delet='<%=request.getParameter("delete")%>';
    if(delet=='yes'){
    alert("删除成功!");
    }
    </script>
    <form action="servlet?method=delete" method="post">
    <table align="center">
    <tr>
    <td>请输入要删除商品的名称:</td>
    <td>
    <input type="text" name="name">
    </td>
    </tr>
    <tr align="center">
    <td>
    <input type="submit" value="确定">
    <input type="submit" value="返回" formaction="gongneng.jsp" formmethod="post">
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>修改分数页面</title>
    </head>
    <body>
    <script>
    var update='<%=request.getParameter("update")%>';
    if(update=='yes'){
    alert("修改成功!");
    }
    </script>
    <form action="servlet?method=update" method="post">
    <table align="center">
    <tr>
    <td>请输入要修改商品名称:</td>
    <td>
    <input type="text" name="name">
    </td>
    </tr>
    <tr>
    <td>请输入要修改后的商品名称:</td>
    <td>
    <input type="text" name="name1">
    </td>
    </tr>
    <tr>
    <td>请输入修改价格:</td>
    <td>
    <input type="text" name="jiage">
    </td>
    </tr>
    <tr>
    <td>请输入修改数量:</td>
    <td>
    <input type="text" name="shuliang">
    </td>
    </tr>
    <tr align="center">
    <td>
    <input type="submit" value="确定">
    <input type="submit" value="返回" formaction="gongneng.jsp" formmethod="post">
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>显示页面</title>
    </head>
    <body>
    <form>
    <table>
    <tr>
    <td>商品名称</td>
    <td>商品价格</td>
    <td>数量</td>
    <td>选择</td>
    </tr>
    <tr>
    <c:forEach items="${list}" var="item">
    <tr>
    <td>${item.name}</td>
    <td>${item.jiage}</td>
    <td>${item.shuliang}</td>
    <td><input type="submit" value="选择" formaction="people.jsp" formmethod="post"></td>
    </tr>
    </c:forEach>
    </tr>
    <tr>
    <td>
    <input type="submit" value="返回" formaction="gongneng.jsp" formmethod="post">
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>认证界面</title>
    </head>
    <body>
    <script>
    var add='<%=request.getParameter("add")%>';
    if(add=="yes"){
    alert("添加成功!");
    }
    </script>
    <form action="servlet?method=login&name=name1" method="post">
    <table align="center">

    <tr>
    <td>名字:</td>
    <td>
    <input type="text" name="mingzi">
    </td>
    </tr>
    <tr>
    <td>性别:</td>
    <td>
    <input type="text" name="sex">
    </td>
    </tr>
    <tr>
    <td>年龄:</td>
    <td>
    <input type="text" name="age">
    </td>
    </tr>
    <tr align="center">
    <td>
    <input type="submit" value="确定" >
    <input type="submit" value="返回" formaction="people.jsp" formmethod="post">
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>删除商品页面</title>
    </head>
    <body>
    <script>
    var delet='<%=request.getParameter("deletes")%>';
    if(delet=='yes'){
    alert("购买成功!");
    }
    </script>

    </body>
    </html>

    package com.pp;
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.List;
    public class dao {
    public void insert(shang L) {
    String sql="insert into shangpin(name,jiage,shuliang) values(?,?,?)";
    Connection con;
    try {
    con = DBUtil.getConnection();
    PreparedStatement pa=con.prepareStatement(sql);
    pa.setString(1,L.getName());
    pa.setString(2,L.getJiage());
    pa.setString(3,L.getShuliang());

    pa.execute();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    public void delete(String name) {
    String sql="delete from shangpin where name=?";
    Connection con;
    try {
    con = DBUtil.getConnection();
    PreparedStatement pa=con.prepareStatement(sql);
    pa.setString(1,name);
    pa.execute();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    public void update(String name1,String name,String jiage,String shuliang) {
    String sql="update shangpin set name=? , jiage=? , shuliang=? where name=?";
    Connection con;
    try {
    con = DBUtil.getConnection();
    PreparedStatement pa=con.prepareStatement(sql);
    pa.setString(1,name1);
    pa.setString(2,jiage);
    pa.setString(3,shuliang);
    pa.setString(4,name);


    pa.execute();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    public List<shang> select() {
    String sql="select * from shangpin";
    shang L=null;
    List<shang> list=new ArrayList();
    Connection con;
    try {
    con = DBUtil.getConnection();
    PreparedStatement pa=con.prepareStatement(sql);
    ResultSet rs=pa.executeQuery();
    while(rs.next()) {
    String jiage=rs.getString("jiage");
    String name=rs.getString("name");
    String shuliang=rs.getString("shuliang");

    L=new shang(name,jiage,shuliang);
    list.add(L);
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return list;
    }
    // public boolean selectp(String num) {
    // boolean panduan=false;
    // String sql="select * from password";
    // Connection con;
    // try {
    // con = DBUtil.getConnection();
    // PreparedStatement pa=con.prepareStatement(sql);
    // ResultSet rs=pa.executeQuery();
    // while(rs.next()) {
    // String numm=rs.getString("num");
    // String password=rs.getString("password");
    // if(num.equals(numm)&&num.equals(password)) {
    // panduan=true;
    // break;
    // }
    // }
    // } catch (SQLException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // return panduan;
    // }
    }

    package com.pp;
    import java.sql.*;
    public class DBUtil{
    private DBUtil() {}
    static {
    try {
    Class.forName("com.mysql.jdbc.Driver");
    }catch(ClassNotFoundException e) {
    e.printStackTrace();
    }
    }
    public static Connection getConnection() throws SQLException{
    return DriverManager.getConnection("jdbc:mysql://localhost:3306/shujuku?serverTimezone=UTC&useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8","root","123456");
    }
    public static void close(Connection conn,Statement ps,ResultSet rs) {
    if(rs!=null) {
    try {
    rs.close();
    }catch(SQLException s) {
    s.printStackTrace();
    }
    }
    if(ps!=null) {
    try {
    ps.close();
    }catch(SQLException s) {
    s.printStackTrace();
    }
    }
    if(conn!=null) {
    try {
    conn.close();
    }catch(SQLException s) {
    s.printStackTrace();
    }
    }
    }
    }

    package com.pp;

    import java.io.IOException;
    import java.util.List;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
    * Servlet implementation class servlet
    */
    @WebServlet("/servlet")
    public class servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
    * @see HttpServlet#HttpServlet()
    */
    public servlet() {
    super();
    // TODO Auto-generated constructor stub
    }
    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String shuliang=request.getParameter("shuliang");
    String name=request.getParameter("name");
    String jiage=request.getParameter("jiage");

    shang stu=new shang(name,jiage,shuliang);
    dao L=new dao();
    L.insert(stu);
    response.sendRedirect("add.jsp?add=yes");
    }
    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String name=request.getParameter("name");

    dao L=new dao();
    L.delete(name);
    response.sendRedirect("delete.jsp?delete=yes");
    }
    private void delete1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String name=request.getParameter("name");

    dao L=new dao();
    L.delete(name);
    response.sendRedirect("deletes.jsp?deletes=yes");
    }
    private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String name=request.getParameter("name");
    String jiage=request.getParameter("jiage");
    String shuliang=request.getParameter("shuliang");
    String name1=request.getParameter("name1");
    dao L=new dao();
    L.update(name1,name,jiage,shuliang);
    response.sendRedirect("update.jsp?update=yes");
    }
    private void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    dao L=new dao();
    String name=request.getParameter("name");

    List<shang> list=L.select();

    request.setAttribute("list",list);
    request.getRequestDispatcher("show.jsp").forward(request, response);
    }
    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    String name=request.getParameter("name");
    String name2=request.getParameter("mingzi");
    String sex=request.getParameter("sex");
    String age=request.getParameter("age");
    if("潘福龙".equals(name2)&&"男".equals(sex)&&"20".equals(age)) {
    request.getSession().setAttribute("name",name);
    request.getRequestDispatcher("servlet?method=delete1").forward(request, response);
    }

    }
    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    request.setCharacterEncoding("UTF-8");
    String method=request.getParameter("method");
    if("add".equals(method)) {
    add(request,response);
    }
    else if("delete".equals(method)) {
    delete(request,response);
    }
    else if("update".equals(method)) {
    update(request,response);
    }
    else if("show".equals(method)) {
    show(request,response);
    }
    else if("delete1".equals(method)) {
    delete1(request,response);
    }
    else if("login".equals(method)) {
    login(request,response);
    }


    }

    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    }

    }

    package com.pp;

    public class shang {

    private String name;
    private String jiage;
    private String shuliang;


    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getJiage() {
    return jiage;
    }
    public void setJiage(String jiage) {
    this.jiage = jiage;
    }
    public String getShuliang() {
    return shuliang;
    }
    public void setShuliang(String shuliang) {
    this.shuliang = shuliang;
    }
    public shang(String name, String jiage, String shuliang) {
    super();
    this.jiage=jiage;
    this.name = name;
    this.shuliang = shuliang;
    }
    }

  • 相关阅读:
    6th week blog3
    6th week blog2(颜色)
    6th week blog1(CSS单位)
    布局—一侧固定宽度,一侧自适应
    布局—两侧固定,中间自适应
    九宫格
    选项卡
    缓冲运动框架
    封装一些常用的js工具函数-不定时更新(希望大家积极留言,反馈bug^_^)
    在一定[min,max]区间,生成n个不重复的随机数的封装函数
  • 原文地址:https://www.cnblogs.com/092e/p/14142901.html
Copyright © 2020-2023  润新知