weixin_40980892
weixin_40980892
采纳率50%
2017-11-07 16:05 阅读 1.6k
已采纳

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 sinat_38913556 2017-11-08 00:12
    List<student> list=(List)request.getAttribute("list");
    
    

    你这里应该带泛型啊

    点赞 1 评论 复制链接分享
  • qq_23126581 郭老师的小迷弟雅思莫了 2017-11-08 00:46

    楼上正解,还有你这代码写的不规则啊,后台的代码最好在前台不要出现,还有你数据库加载,用配置文件就行了。当然刚开始接触java的例外。

    点赞 评论 复制链接分享
  • weixin_40980892 weixin_40980892 2017-11-08 01:42

    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); } student stu=new student(); stu.setUser("jack"); stu.setPass("123456"); stu.setNick("sasa"); list.add(stu); request.setAttribute("list", list);

    点赞 评论 复制链接分享
  • qq_23126581 郭老师的小迷弟雅思莫了 2017-11-08 01:49

    你用el表达式获取看看,比如:${list} 或者${request.list}

    点赞 评论 复制链接分享
  • weixin_40980892 weixin_40980892 2017-11-08 07:29

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

    点赞 评论 复制链接分享
  • weixin_40980892 weixin_40980892 2017-11-08 07:38

    org.apache.jasper.JasperException: Unable to compile class for JSP:

    An error occurred at line: [30] in the jsp file: [/output.jsp]
    Syntax error, insert "}" to complete Statement
    27: if (request.getParameter("username").equals(student.getUser()) && request.getParameter("password").equals(student.getPass()) ) {
    28: {
    29: nickname = student.getNick();
    30: }
    31: else{
    32: nickname="瀵嗙爜閿欒";
    33: }

    An error occurred at line: [172] in the generated java file: [C:\Users\Administrator.IntelliJIdea2017.2\system\tomcat_untitled\work\Catalina\localhost\ROOT\org\apache\jsp\output_jsp.java]
    Syntax error on token "}", delete this token

    An error occurred at line: [173] in the generated java file: [C:\Users\Administrator.IntelliJIdea2017.2\system\tomcat_untitled\work\Catalina\localhost\ROOT\org\apache\jsp\output_jsp.java]
    Syntax error, insert "}" to complete ClassBody

    Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:213)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:458)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:389)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:345)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:603)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    Student.MyServlet.doPost(MyServlet.java:60)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

    点赞 评论 复制链接分享
  • weixin_40980892 weixin_40980892 2017-11-08 07:39

    Type Exception Report

    Message An exception occurred processing [/output.jsp] at line [27]

    Description The server encountered an unexpected condition that prevented it from fulfilling the request.

    Exception

    org.apache.jasper.JasperException: An exception occurred processing [/output.jsp] at line [27]

    24: }
    25: else {
    26: for (student student : list) {
    27: if (request.getParameter("username").equals(student.getUser()) && request.getParameter("password").equals(student.getPass()) )
    28: {
    29: nickname = student.getNick();
    30: }

    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:593)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:482)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    Student.MyServlet.doPost(MyServlet.java:60)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    Root Cause

    java.lang.NullPointerException
    org.apache.jsp.output_jsp._jspService(output_jsp.java:137)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:444)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    Student.MyServlet.doPost(MyServlet.java:60)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    Note The full stack trace of the root cause is available in the server logs.

    Apache Tomcat/9.0.1

    点赞 评论 复制链接分享
  • weixin_40980892 weixin_40980892 2017-11-08 07:49

    <%--
    Created by IntelliJ IDEA.
    User: Administrator
    Date: 2017/11/6
    Time: 14:15
    To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>


    登录界面



    用户名:


    密 码:





    点赞 评论 复制链接分享

相关推荐