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>
以上为一些主要的代码 主要还是做一个自己学习的记录 有很多不足的地方
servlet+jsp+xml写的一个小型的商品展示页面
最新推荐文章于 2021-06-29 21:09:48 发布