weixin_40980892 2017-11-07 08: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-07 16:12
    关注
    List<student> list=(List)request.getAttribute("list");
    
    

    你这里应该带泛型啊

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

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部