servlet+jsp+xml写的一个小型的商品展示页面

package Local_Servlet;

import org.apache.catalina.Session;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/login")
public class Login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("gbk");
        resp.setCharacterEncoding("gbk");

        String name = req.getParameter("name");
        String password = req.getParameter("password");
        File file = new File("D:\\Codes——IDea\\web\\Users.xml");
        SAXReader reader = new SAXReader();
        Document document=null;
        String Xpath="//user[@name='"+name+"' and "+"@password='"+password+"']";
        HttpSession session = req.getSession();
        System.out.println(Xpath);
        try {
            document = reader.read(file);
            System.out.println(document);

        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Node node = document.selectSingleNode(Xpath);
        if(node==null){
            resp.getWriter().write("账号密码错误 请重新登陆");
            session.setAttribute("state",null);

        }else{
            session.setAttribute("state",name);
            resp.sendRedirect("index.jsp");
        }

    }
}


<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2017/9/17
  Time: 12:59
  To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="Mytag" uri="http://vmaxtam.com/mytag" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=GBK" language="java" %>
<html>
<head>
    <title>商品展示页面</title>
</head>
<body>

<div align="center" >
    <h1>商品展示页面</h1>
</div>
<div align="left">用户:<a href="login.jsp"><Mytag:GetLoginState></Mytag:GetLoginState></a>
    <c:if test="${!empty sessionScope.state}">
        <a href="/add">添加商品</a>
        <hr/>
        <Mytag:GetItems></Mytag:GetItems>
        <c:forEach var="item1" items="${item}">
            <table border="1px" cellpadding="2" cellspacing="1" width="300px" align="left">
                <tr><td colspan="2" align="center">商品ID:${item1.ID}</td></tr>
                <tr><td colspan="2" align="center">商品名称:${item1.name}</td></tr>
                <tr><td colspan="2" align="center">商品类别:${item1.type}</td></tr>
                <tr><td colspan="2" align="center">商品价格:${item1.price}</td></tr>
                <tr><td align="center"><a href="/delete?id=${item1.ID}">删除</a> </td><td align="center"><a href="">修改</a> </td></tr>
            </table>
        </c:forEach>
</c:if>
</div>
<hr/>


</body>
</html>



<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2017/9/16
  Time: 22:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <style type="text/css">
      #a1{
        background-image: url("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2282833641,3720218379&fm=27&gp=0.jpg");
        background-size: cover;
      }
      #a2{
        background-image: url("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2282833641,3720218379&fm=27&gp=0.jpg");
        background-size: cover;
        margin-top: 17%;
      }
    </style>
    <title>登陆</title>
  </head>
  <body id="a1">
<div align="center" >

    <table bgcolor="black" border="2px" width="300px" id="a2">
      <form action="/login" method="post">
      <tr>
        <td colspan="2"><input type="text" name="name" style="width: 100%"/></td>
      </tr>
      <tr>
        <td colspan="2"> <input type="password" name="password" style="width:100%"/></td>
      </tr>
      <tr>
        <td align="center"><input type="submit" name="login" value="登陆" style="width:100%"></td>
      </form>
        <td align="center"><button id="reg" style="width:100%" οnclick="window.open('/reg.jsp')">注册</button>  </td>
      </tr>

    </table>
</div>
  </body>
</html>

以上为一些主要的代码  主要还是做一个自己学习的记录  有很多不足的地方
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值