JSP中<%%>能把if和else分开吗?

tmhk123 2010-10-15 06:43:13
我见有这种写法

<%if(session.getAttribute("username")==null){%>
<td width="153" height="17"><div align="center">用户注册</div></td>
<td width="149"><div align="center">用户登录</div></td>
<%}else{ %>
<td width="153" height="17"><div align="center">签写留言</div></td>
<td width="149"><div align="center">修改资料</div></td>
<%} %>


然后我这样写,就出错了

<%int c=1;
Object count = application.getAttribute("count");
if(count ==null)
{application.setAttribute("count",c);}%>
<%
else{application.setAttribute
("count",(Integer)(count)+1);}
%>


JSP中<%%>能把if和else分开吗?
...全文
1056 点赞 收藏 11
写回复
11 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
tmhk123 2010-10-16
解决了 顶8楼
回复
yaminor 2010-10-16
bbbbbbbbbbb
回复
滚球屎壳螂 2010-10-16
想问一下,在JSP中,如果加入了javascript方法,能在javascript方法中加入<% %>脚本吗?
回复
jumpheightway 2010-10-16
完全可以的
但是必须要有括号做索引
否则会报语法错误
回复
tmhk123 2010-10-16
[Quote=引用 4 楼 fan1989800 的回复:]
type Exception report

message
Syntax error on token "else", delete this token
从上面的报错来看,需要对count的类型再作判断,并不是count!=null就说明count就能转换为int型
[/Quote]
为什么我把if else之间的%> <%去掉了就好了呢?
回复
tmhk123 2010-10-16
[Quote=引用 4 楼 fan1989800 的回复:]
type Exception report

message
Syntax error on token "else", delete this token
从上面的报错来看,需要对count的类型再作判断,并不是count!=null就说明count就能转换为int型
[/Quote]

为什么我把if和else之间的%> <%去掉就好了呢??
回复
voice1122 2010-10-16

<%int c=1;
Integer count = (Integer)application.getAttribute("count");
if(count == null){
application.setAttribute("count",new Integer(c));
}
%>

<% else{
application.setAttribute("count",new Integer(count.intValue()+1));
}
%>

改了下,LZ看看有用没
回复
new_java水手 2010-10-15
type Exception report

message
Syntax error on token "else", delete this token
从上面的报错来看,需要对count的类型再作判断,并不是count!=null就说明count就能转换为int型

回复
tmhk123 2010-10-15
[Quote=引用 1 楼 villagehead 的回复:]
你不是已经分开了吗?

出什么错了?贴上来看看

good luck
[/Quote]

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

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

An error occurred at line: 7 in the jsp file: /Counter.jsp
Syntax error on token "else", delete this token
4: <%Object count = application.getAttribute("count");%>
5: <%if(count ==null)
6: {application.setAttribute("count",c);}%>
7: <%else{application.setAttribute
8: ("count",(Integer)(count)+1);}
9: %>


Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:349)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:968)
org.apache.jsp.Index1_jsp._jspService(Index1_jsp.java:59)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


回复
xtawgipl 2010-10-15
能得不能再能了...
回复
villagehead 2010-10-15
你不是已经分开了吗?

出什么错了?贴上来看看

good luck
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2010-10-15 06:43
社区公告
暂无公告