JSP Unreachable code
难道是return提前结束程序导致 %> 没编译到?但他不是程序片标记吗?
代码是按书上写的,书上代码在最后
login.jsp
<%--
Created by IntelliJ IDEA.
User: 26316
Date: 2023/3/29
Time: 10:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
<title>后台登录</title>
<link href="${pageContext.request.contextPath}/sys/style/css/login.css" type="text/css" rel="stylesheet">
</head>
<body>
<div class="login">
<div class="message">ChrosWine红酒有限公司-管理登录</div>
<div id="darkbannerwrap"></div>
<form action="${pageContext.request.contextPath}/sys/action.jsp" method="post">
<input name="name" placeholder="用户名" required="" type="text">
<hr class="hr15">
<input name="password" placeholder="密码" required="" type="password">
<hr class="hr15">
<input value="登录" style="width: 100%;" type="submit">
</form>
</div>
<div class="copyright"></div>© 2018红酒有限公司<a href="http://www.bjjqe.com/" target="_blank">金企鹅联合出版社</a>
</body>
</html>
action.jsp
```html
<%--
Created by IntelliJ IDEA.
User: 26316
Date: 2023/3/29
Time: 10:54
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="util.DBHelper" %>
<%@ page import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
String name = request.getParameter("name");//用户名
String password = request.getParameter("password");//密码
//判断用户名或密码是否为空
if (name == null || "".equals(name.trim()) || password == null || "".equals(password.trim())){
System.out.println("用户名或密码不能为空!");
response.sendRedirect("login.jsp");
return;
}
boolean isValid = false; //定义isValid变量为布尔值false
PreparedStatement stmt = null; //语句对象
ResultSet rs = null; //结果集对象
try {
String sql = "select * from administrators where name=? and password=?;"; //SQL语句
stmt = DBHelper.getConnection().prepareStatement(sql); //创建链接对象
//设置SQL语句参数
stmt.setString(1, name);
stmt.setString(2, password);
rs = stmt.executeQuery(); //获得数据集
//如果rs有值,则变量isValid的值变为true
if (rs.next()){
isValid = true;
}
}catch (Exception e){
e.printStackTrace();
}finally {
//释放数据集对象
if (rs != null){
try {
rs.close();
rs = null;
}catch (Exception ex){
ex.printStackTrace();
}
}
//释放语句对象
if (stmt != null){
try {
stmt.close();
stmt = null;
}catch (Exception ex){
ex.printStackTrace();
}
}
}
//判断是否登录成功
if (isValid){
System.out.println("登录成功!");
session.setAttribute("name", name);
response.sendRedirect("../sys/index.jsp");
return;
}else {
System.out.println("登录失败!");
response.sendRedirect("../sys/login.jsp");
return;
}
%>
</body>
</html>
书上源码
```html
<%@page import="util.DBHelper"%>
<%@page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%
String name = request.getParameter("name"); //用户名
String password = request.getParameter("password"); //密码
//判断用户名或密码是否为空
if (name == null || "".equals(name.trim()) || password == null || "".equals(password.trim())) {
System.out.println("用户名或密码不能为空!");
response.sendRedirect("login.jsp");
return;
}
boolean isValid = false; //定义isValid变量为布尔值false
PreparedStatement stmt = null; // 语句对象
ResultSet rs = null; // 结果集对象
try {
String sql = "select * from administrators where name=? and password=?;"; //SQL语句
stmt = DBHelper.getConnection().prepareStatement(sql); //创建链接对象
stmt.setString(1, name); //设置SQL语句的第一个参数用户名的值
stmt.setString(2, password); //设置SQL语句第二个参数密码的值
rs = stmt.executeQuery(); //获得数据集
if (rs.next()) { //如果rs有值,则执行
isValid = true; //变量isValid的值变为true
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放数据集对象
if (rs != null) {
try {
rs.close();
rs = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 释放语句对象
if (stmt != null) {
try {
stmt.close();
stmt = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
//判断是否登录成功
if (isValid) {
System.out.println("登录成功!");
session.setAttribute("name", name);
response.sendRedirect("../sys/index.jsp");
return;
} else {
System.out.println("登录失败!");
response.sendRedirect("../sys/login.jsp");
return;
}
%>