qq_57931996 2023-03-30 10:26 采纳率: 88.9%
浏览 34
已结题

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

  • 写回答

4条回答 默认 最新

  • CSDN专家-showbo 2023-03-30 10:44
    关注

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 4月7日
  • 已采纳回答 3月30日
  • 创建了问题 3月30日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改