weixin_40980892 2017-11-07 16:05 采纳率: 50%
浏览 1684
已采纳

jsp界面ArrayList取不到值,直接新建对象加入也传输不到下个界面

package Student;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import Student.student;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {

        Class.forName("org.gjt.mm.mysql.Driver");

        String url = "jdbc:mysql://localhost:3306/tset";

        String username = "root";

        String password = "123456";

        Connection conn = DriverManager.getConnection(url, username, password);

        Statement stmt = conn.createStatement();

        String sql = "select * from student ";

        ResultSet rs = stmt.executeQuery(sql);

        List<student> list = new ArrayList<student>();


        while(rs.next()){
            student stu=new student();
            stu.setUser(rs.getString("user"));
            stu.setPass(rs.getString("pass"));
            stu.setNick(rs.getString("nick"));
            list.add(stu);
    }
        request.setAttribute("list", list);
        rs.close();
        stmt.close();
        conn.close();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    request.getRequestDispatcher("output.jsp").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request,response);
}

}

<%@ page import="java.sql.Connection" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2017/11/7
Time: 16:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="Student.student"%>



欢迎

<%
List list=(List)request.getAttribute("list");
String nickname="sss";
if (list == null) {
nickname="不存在这人";
}
else {
for (student student : list) {
if (request.getParameter("username").equals(student.getUser()) && request.getParameter("password").equals(student.getPass()) ) {
nickname = student.getNick();

        }
        else{
            nickname="密码错误";
        }
    }
}

%>
<%=nickname%>


  • 写回答

8条回答 默认 最新

  • sinat_38913556 2017-11-08 00:12
    关注
    List<student> list=(List)request.getAttribute("list");
    
    

    你这里应该带泛型啊

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

报告相同问题?

悬赏问题

  • ¥15 树莓派启动AP热点传入数据
  • ¥15 multisim中关于74ls192n和DSWPK开关的问题(相关搜索:计数器)
  • ¥15 在误装Windows server2019 后如何利用Windows.old恢复?
  • ¥20 代码实现状态连接包过滤防火墙的设计与实现
  • ¥15 vscode的红色箭头爆红和has no default export报错
  • ¥15 关于#sql#的问题:#情况描述 在用vs对项目进行调试时,出现找不到网络路径,然后查看SQL配置工具,发现SQL服务显示远程调用过程失败(相关搜索:防火墙)
  • ¥15 eNSP中基于默认路由及浮动路由的公司与分部互联和校园网综合项目
  • ¥15 主要进行描述泥浆在管路不同区段泥浆的密度不相同,泥浆的密度有高有低,此时管路的摩阻分布需要怎么计算,(标签-matlab)
  • ¥40 通过编制程序计算圆管内层流充分发展对流换热,参数如图5-4,以及公式5-16所示,要求用表5-6对程序计算准确性进行验证
  • ¥20 该学习哪个编程语言? AI会取代程序员吗?