<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import = "java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>数据处理页面</title> </head> <body> <% String userName = new String(request.getParameter("userName").getBytes("ISO-8859-1"),"UTF-8"); String password = new String(request.getParameter("password").getBytes("ISO-8859-1"),"UTF-8"); Statement st = null; Connection con = null; ResultSet rs = null; if(userName.equals("")){ response.sendRedirect("login.jsp"); } try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String url = "jdbc:sqlserver://localhost:1433;DatabaseName=eims;"; con = DriverManager.getConnection(url, "sa", "2017212027"); st = con.createStatement(); String sql = "select * from sqlUser where userName = '"+userName+"'"; rs = st.executeQuery(sql); if(rs.next()){ String sql1 = "select * from sqlUser where password = '"+password+"'"; rs = st.executeQuery(sql1); if(rs.next()){ response.sendRedirect("main/main.jsp"); }else{ response.sendRedirect("login.jsp"); } } }catch(Exception e){ e.printStackTrace(); }finally{ rs.close(); st.close(); con.close(); } %> </body> </html>
时间: 2023-07-19 17:49:41 浏览: 2339
这是一个JSP页面,用于处理用户登录信息。页面首先获取用户名和密码,然后连接数据库,查询是否有该用户名,如果有则再次查询该用户是否有相应的密码。如果两次查询都通过,则将用户重定向到主页面,否则重定向到登录页面。
需要注意的是,在获取用户名和密码时,使用了request.getParameter()方法,并将获取到的字节数组转换为UTF-8格式的字符串。这是因为浏览器默认使用ISO-8859-1编码方式,而在处理中文字符时需要使用UTF-8编码方式。
此外,代码使用了JDBC技术连接数据库,并执行了SQL语句。需要注意的是,代码中没有对SQL注入进行处理,存在安全风险。建议在实际应用中使用预编译的SQL语句或其他安全措施来避免此类问题。
相关问题
优化一下这条代码:<%@page language="java"import="java.util.*"pageEncoding="GBK"%> <%@page import="jspSamples.unit7.websiteSample.*"%>
优化后的代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*, jspSamples.unit7.websiteSample.*" %>
主要改动如下:
1. 设置页面编码为 UTF-8,以支持更多字符集;
2. 将 import 语句合并到一个语句中,减少页面代码行数。
<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="utf-8"%> <%@page import="com.util.SQLConn"%> <%@page import="org.apache.commons.lang3.*" %> <%@page import="com.util.LOGS" %> <html> <body> <%-- 获取前端数据 --%> <% String username = request.getParameter("username"); String usernum = request.getParameter("usernum"); String name=request.getParameter("ytime"); String mon=request.getParameter("mtime"); String num=request.getParameter("rtime"); String people = request.getParameter("people"); %> <%-- 定义SQLConn类和INSERT_SQL常量 --%> <% final String INSERT_SQL = "INSERT INTO YuYue(UserID, Time, People) VALUES (?, ?, ?)"; %> <%-- 执行数据库操作 --%> <% int count = 0; try {Connection conn = SQLConn.openDB(); PreparedStatement pstmt = conn.prepareStatement(INSERT_SQL); { pstmt.setString(1, usernum); pstmt.setString(2, name+mon+num); pstmt.setString(3, people); count = pstmt.executeUpdate(); }} catch (SQLException e) { e.printStackTrace(); } %> <%-- 根据执行结果进行相应的操作 --%> <c:choose> <c:when test="${count > 0}"> <%-- 成功跳转到ZCS.jsp --%> <c:redirect url="ZCS.jsp"/> </c:when> <c:otherwise> <%-- 失败跳转到ZCB.jsp --%> <c:redirect url="../ZCB.jsp"/> </c:otherwise> </c:choose> </body> </html>优化代码
以下是对代码进行优化的建议:
1. 在第二行中,导入语句中的星号通配符应该避免使用,应该明确导入需要使用的类或接口。
2. 在第十行中,可以将 INSERT_SQL 常量定义在 SQLConn 类中并进行调用,以提高代码的可维护性和复用性。
3. 在第十二行中,PreparedStatement 对象应该使用 try-with-resources 语句,以自动释放资源并避免资源泄露问题。
4. 在第十六至二十二行中,可以使用 EL 表达式代替 JSTL 标签,以简化代码并提高可读性。
基于上述建议,优化后的代码如下:
```
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*, java.sql.*"%>
<%@ page import="com.util.SQLConn"%>
<%@ page import="com.util.LOGS"%>
<%@ page import="org.apache.commons.lang3.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert Data</title>
</head>
<body>
<%-- 获取前端数据 --%>
<% String username = request.getParameter("username");
String usernum = request.getParameter("usernum");
String name = request.getParameter("ytime");
String mon = request.getParameter("mtime");
String num = request.getParameter("rtime");
String people = request.getParameter("people"); %>
<%-- 执行数据库操作 --%>
<% int count = 0;
try (Connection conn = SQLConn.openDB();
PreparedStatement pstmt = conn.prepareStatement(SQLConn.INSERT_SQL)) {
pstmt.setString(1, usernum);
pstmt.setString(2, name + mon + num);
pstmt.setString(3, people);
count = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} %>
<%-- 根据执行结果进行相应的操作 --%>
<c:if test="${count > 0}">
<%-- 成功跳转到 ZCS.jsp --%>
<jsp:forward page="ZCS.jsp"></jsp:forward>
</c:if>
<c:if test="${count <= 0}">
<%-- 失败跳转到 ZCB.jsp --%>
<jsp:forward page="../ZCB.jsp"></jsp:forward>
</c:if>
</body>
</html>
```
优化后的代码更加简洁、易读、易维护,并且避免了可能存在的一些问题。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="search.user" import="search.ticket"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body bgcolor=pink>
录入成功!
<% user user=(user)session.getAttribute("user"); ticket[] tickets=(ticket[])session.getAttribute("tickets"); %> <% for(int i=0;i<tickets.length;i++){ out.println(tickets[i].getId()+" "+tickets[i].getWherestart()+" "+tickets[i].getGotowhere()+" "+ tickets[i].getStart_time()+" "+tickets[i].getAirEnterPrice()+"
"); } %> 发送</a--> </body> </html>如果要把tickets数组发送过去该怎么改
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="com.test.bean.Goods,java.util.ArrayList" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>购物车</title> <style type="text/css"> table{border-collapse:collapse;} td{border:1px solid black; text-align:center; } #deal{margin-left:200px} </style> </head> <body> <jsp:useBean id="cart" class="com.test.bean.Cart" scope="session"></jsp:useBean> <%if(cart==null||cart.getGoodslist().size()==0) out.println("购物车空空如也.....返回商品首页"); else{ ArrayList<Goods>goodslist=cart.getGoodslist(); %> 当前购物车共有<%=cart.getGcount() %>件物品 返回商品首页
序号 商品号 商品名称 价格 数量 小计 描述 <%for(int i=0;i<goodslist.size();i++){ Goods goods=goodslist.get(i); %> <%=i+1 %> <%=goods.getGid() %> <%=goods.getGname() %> <%= String.format("%.2f", goods.getGprice())%> <%=goods.getGcount() %> <%=String.format("%.2f", goods.getGprice()*goods.getGcount()) %> 删除 <%}%>
总计<%=String.format("%.2f",cart.getTotal())%> <input type="button" id="deal" value="购买" /></input> <%}%> <script type="text/javascript"> var dealbutton=document.getElementById("deal"); dealbutton.onclick=function(){ var cf=confirm("确定购买吗?"); if(cf==true){ window.location.href="Servlet/DealServlet"; } } </script> </body> </html> 修改删除功能,在购物车物品数量大于1的情况下点击“删除”时减少1个,等于1时删除此项商品;
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)