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分开吗?
...全文
1263 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
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

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧