JSP中<base href="<%=basePath%>">作用

通常在JSP页面开通有如下代码:

这段代码的意思是获取当前项目的路径,如:http://localhost:8080/项目名称。

 

在<head></head>中通常有如下代码:

 

这是设置基础路径的,basePath为变量,简单的静态网页的话你设置比如:<base href="http://www.baidu.com">,那你下面的href属性就会以你上面设的为基准,如:<a href="http://www.baidu.com/xxx.htm"></a>你现在就只需要写<a href="xxx.htm"></a>

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@page import="com.util.SQLConn"%> <%@page import="java.sql.*" %> <%@page import="java.sql.Statement" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'ZC.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% //获取前端数据 String usr = request.getParameter("username"); String num = request.getParameter("usernum"); String pwd = request.getParameter("userpwd"); //创建数据库类的对象 SQLConn.openDB(); //insert into User_ZC(Name,Code,Number,Power) values('usr的变量','num的变量','pwd的变量','1'); String sql = "insert into User_ZC(UserID,Name,Code,Power) values('"+num+"','"+usr+"','"+pwd+"','"+1+"')"; System.out.print("sql:"+sql); //执行SQL语句 if(SQLConn.execUpdate(sql) != 0 &&!"".equals(SQLConn.execUpdate(sql))){ int n = SQLConn.execUpdate(sql); System.out.print("n:"+n); if(n != 0 ){ request.getRequestDispatcher("ZCS.jsp").forward(request,response); }else{ request.getRequestDispatcher("ZCB.jsp").forward(request,response); } }else{System.out.print("执行SQL语句失败");} //关闭数据库 SQLConn.closeDB(); %> </body> </html>,优化代码
05-26
下面是对这段 JSP 代码的优化: 1. 在 SQL 语句中使用占位符,防止 SQL 注入攻击。 2. 在执行 SQL 语句时,判断执行结果是否成功,并根据执行结果进行相应的操作。同时,在使用 SQLConn.execUpdate() 方法时,应该将其结果保存在一个变量中,避免重复执行 SQL 语句。 3. 删除无用的注释,保留必要的注释,并且注释应该清晰、简洁、易懂。 4. 在获取前端数据时,进行非空判断,避免空指针异常。 5. 使用 try-catch-finally 块来处理数据库连接的异常,并在 finally 块中关闭数据库连接。 6. 将 HTML 和 Java 代码分离,采用 MVC 设计模式来组织代码。 7. 使用 JSTL 和 EL 表达式来替代 Java 代码中的逻辑判断和字符串拼接。 8. 将数据库连接信息、SQL 语句等信息抽象出来,放到配置文件中,以便于维护和修改。 下面是优化后的代码: <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@page import="com.util.SQLConn"%> <%@page import="java.sql.*" %> <%@page import="java.sql.Statement" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <% // 获取前端数据 String username = request.getParameter("username"); String usernum = request.getParameter("usernum"); String userpwd = request.getParameter("userpwd"); // 执行数据库操作 Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; int count = 0; try { conn = SQLConn.getConnection(); String sql = "INSERT INTO User_ZC(UserID, Name, Code, Power) VALUES (?, ?, ?, ?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, usernum); pstmt.setString(2, username); pstmt.setString(3, userpwd); pstmt.setInt(4, 1); count = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { SQLConn.close(conn, pstmt, rs); } // 根据执行结果进行相应的操作 if (count > 0) { request.getRequestDispatcher("ZCS.jsp").forward(request,response); } else { request.getRequestDispatcher("ZCB.jsp").forward(request,response); } %>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值