jsp代码错误。session的问题。java
一下代码,出现错误。<%@pagelanguage="java"import="java.util.*,com.pkland.md5.*,javax.servlet.ht...
一下代码,出现错误。
<%@ page language="java" import="java.util.*,com.pkland.md5.*,javax.servlet.http.HttpServletResponse.*,javax.servlet.http.HttpSession.*" pageEncoding="gbk"%>
<%! String formatString(String text){
if(text == null) {
return "";
}
return text;
}
%><%
String userName=formatString(session.getAttribute("userName").toString());//游戏ID()
String source=formatString(session.getAttribute("source").toString()); //渠道ID
String time=formatString(session.getAttribute("time").toString()); //渠道ID
String key="123123"; //双方约定好的Key
String md5str=java.net.URLEncoder.encode(userName+key,"UTF-8");
String token=MD5.toMD5(md5str);
if(userName.equals("")||source.equals("")||time.equals("")){
//登陆错误
}
%>
<html>
<body>
<form action="/lastBattle/eJiaTong/roleDeal.jsp" id="form1" name="form1">
<input type="button" id="zstb_1" value="1服" onclick="select_server(1,1);" /><br>
<input type="button" id="zstb_2" value="2服" onclick="select_server(1,2);" /><br>
<input type="hidden" name="game_id" id="game_id" value="0" />
<input type="hidden" name="server_id" id="server_id" value="0" />
<input type="hidden" name="source" id="source" value=<%=source %> />
<input type="hidden" name="userName" id=userName value=<%=userName %> />
<input type="hidden" name="time" id=time value=<%=time %> />
<input type="hidden" name="token" id=token value=<%=token %> />
</form>
</body>
</html>
<script language="javascript">
function select_server(gameid,serverid){
document.getElementById("game_id").value=gameid;
document.getElementById("server_id").value=serverid;
if(document.getElementById("game_id").value!=""&&document.getElementById("game_id").value!=null&&document.getElementById("server_id").value!=""&&document.getElementById("server_id").value!=null){
//提交表单
form1.submit();
}
}
</script>
错误:
18:31:21,890 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.ejiatong.choose_jsp._jspService(choose_jsp.java:55)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
全贴布上来。应该懂的可以知道。是个空指针异常。请附详细解说。谢谢了。
请说明该怎么解决。谢谢了。 展开
<%@ page language="java" import="java.util.*,com.pkland.md5.*,javax.servlet.http.HttpServletResponse.*,javax.servlet.http.HttpSession.*" pageEncoding="gbk"%>
<%! String formatString(String text){
if(text == null) {
return "";
}
return text;
}
%><%
String userName=formatString(session.getAttribute("userName").toString());//游戏ID()
String source=formatString(session.getAttribute("source").toString()); //渠道ID
String time=formatString(session.getAttribute("time").toString()); //渠道ID
String key="123123"; //双方约定好的Key
String md5str=java.net.URLEncoder.encode(userName+key,"UTF-8");
String token=MD5.toMD5(md5str);
if(userName.equals("")||source.equals("")||time.equals("")){
//登陆错误
}
%>
<html>
<body>
<form action="/lastBattle/eJiaTong/roleDeal.jsp" id="form1" name="form1">
<input type="button" id="zstb_1" value="1服" onclick="select_server(1,1);" /><br>
<input type="button" id="zstb_2" value="2服" onclick="select_server(1,2);" /><br>
<input type="hidden" name="game_id" id="game_id" value="0" />
<input type="hidden" name="server_id" id="server_id" value="0" />
<input type="hidden" name="source" id="source" value=<%=source %> />
<input type="hidden" name="userName" id=userName value=<%=userName %> />
<input type="hidden" name="time" id=time value=<%=time %> />
<input type="hidden" name="token" id=token value=<%=token %> />
</form>
</body>
</html>
<script language="javascript">
function select_server(gameid,serverid){
document.getElementById("game_id").value=gameid;
document.getElementById("server_id").value=serverid;
if(document.getElementById("game_id").value!=""&&document.getElementById("game_id").value!=null&&document.getElementById("server_id").value!=""&&document.getElementById("server_id").value!=null){
//提交表单
form1.submit();
}
}
</script>
错误:
18:31:21,890 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.ejiatong.choose_jsp._jspService(choose_jsp.java:55)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
全贴布上来。应该懂的可以知道。是个空指针异常。请附详细解说。谢谢了。
请说明该怎么解决。谢谢了。 展开
展开全部
String userName=formatString(session.getAttribute("userName").toString());
你这样写其实在还没有formatString之前就已经空指针了。
String userName=session.getAttribute("userName")==null?"":session.getAttribute("userName").toString();
其它那几个变量仿照之中方写就对了。
因为session.getAttribute("userName")如果已经是null了,你再toString()也没有用的就会报null指针异常了,根本走不到formatString()
你这样写其实在还没有formatString之前就已经空指针了。
String userName=session.getAttribute("userName")==null?"":session.getAttribute("userName").toString();
其它那几个变量仿照之中方写就对了。
因为session.getAttribute("userName")如果已经是null了,你再toString()也没有用的就会报null指针异常了,根本走不到formatString()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果一开始userName就没有的话,session.getAttribute("userName")为null,如果加toString()就会报空指针异常
<%! String formatString(Object text){
if(text == null) {
return "";
}
return text.toString();
}
%><%
String userName=formatString(session.getAttribute("userName"));//游戏ID()
String source=formatString(session.getAttribute("source")); //渠道ID
String time=formatString(session.getAttribute("time")); //渠道ID
String key="123123"; //双方约定好的Key
String md5str=java.net.URLEncoder.encode(userName+key,"UTF-8");
String token=MD5.toMD5(md5str);
if(userName.equals("")||source.equals("")||time.equals("")){
//登陆错误
}
%>
<%! String formatString(Object text){
if(text == null) {
return "";
}
return text.toString();
}
%><%
String userName=formatString(session.getAttribute("userName"));//游戏ID()
String source=formatString(session.getAttribute("source")); //渠道ID
String time=formatString(session.getAttribute("time")); //渠道ID
String key="123123"; //双方约定好的Key
String md5str=java.net.URLEncoder.encode(userName+key,"UTF-8");
String token=MD5.toMD5(md5str);
if(userName.equals("")||source.equals("")||time.equals("")){
//登陆错误
}
%>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询