如何在JSP中拆分字符串

时间:2016-07-15 09:16:32

标签: jsp

我在JSP中有一个String,我需要通过特殊的char ,进行拆分。但是我无法拆分它:当尝试下面的代码时,我收到以下错误:

    org.apache.jasper.JasperException: java.lang.NullPointerException
     org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:556)
                                 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
     javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


    root cause 

    java.lang.NullPointerException
    org.apache.jsp.test1jsp_jsp._jspService(test1jsp_jsp.java:115)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)

    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我的代码:

<%@page import="sun.security.util.Length"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"                                                                               
  http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
</head>
<body>
    <%
    String arr1 = request.getParameter("arr");
    String[] a = arr1.split(",");
    for(int x=0; x<a.length; x++)
        out.println(a[x]+"<br>");
    %>
</body>
</html>

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

为什么不把它拆分成Java?例如

<Expander Header="Header">

答案 1 :(得分:0)

您需要认识到存在脚本变量并且存在范围变量的事实。为了使arr1成为范围变量,您需要添加类似

的内容
pageContext.setAttribute("arr1",arr1);   

到你的scriptlet。(我指的是你的初始代码) 实际上,您不需要使用scriptlet。这是演示代码。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"                                                                               
  http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Testing</title>
</head>
<body >
<c:forEach var="splt" items="${fn:split(param.arr1,',')}">
    ${splt} 
</c:forEach>
</body>
</html>