关于Unreachable code的问题,如何解决?

JSP Unreachable code
难道是return提前结束程序导致 %> 没编译到?但他不是程序片标记吗?
代码是按书上写的,书上代码在最后

img

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 + "/";
%>


    Title


<%
    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;
    }
%>




书上源码

```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;
    }
%>

return需要放在函数中吧,删掉。

https://blog.csdn.net/love_life_forever/article/details/83421614

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632

response.sendRedirect后面的有return ;删了就好了

  • 这篇博客: JSP用户登录连接数据库中的 1:创建状态码code 提示信息 返回对象 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • @Getter
    @Setter
    public class ResultInfo<T> {
    
        private Integer code; // 状态码 成功=1,失败=0
        private String msg; // 提示信息
        private T result; // 返回的对象(字符串、JavaBean、集合、Map等)
    
    }