为什么强转出现500错误

u012204355 2013-09-22 09:23:11
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<form action="index.jsp" method="post">
<input type="text" name="num1">
<select name="opt">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" name="num2">
<input type="submit" value="提交"/>
</form>
<%

String str1=request.getParameter("num1");
String str2=request.getParameter("num2");
String str3=request.getParameter("opt");
int a=Integer.parseInt(str1);
int b=Integer.parseInt(str2);
if(str3.equals("+")){
out.print(a+str3+b+"="+(a+b));
}
if(str3.equals("-")){
out.print(a+str3+b+"="+(a-b));
}
if(str3.equals("*")){
out.print(a+str3+b+"="+(a*b));
}
if(str3.equals("/")){
out.print(a+str3+b+"="+(a/b));
}
%>
</body>
</html>

















为什么强转出现500错误
...全文
265 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
u012204355 2013-09-27
  • 打赏
  • 举报
回复
谢了,解决了。缺少个判断
u012204355 2013-09-27
  • 打赏
  • 举报
回复
谢了。解决了,缺少个判断
qiaogn 2013-09-24
  • 打赏
  • 举报
回复
空值吧,代码分离吧建议
MartinHan01 2013-09-24
  • 打赏
  • 举报
回复
建议对可能出错的地方进行捕获异常,看看到底哪出的错,真正项目中不建议jsp文件里面有try catch
partys 2013-09-24
  • 打赏
  • 举报
回复
debug一下
u012204355 2013-09-23
  • 打赏
  • 举报
回复
还是不行啊。那些我都试过
张双磊 2013-09-23
  • 打赏
  • 举报
回复
引用 2 楼 zoeg 的回复:
代码就不看了,就是帮你找到问题,也只是解决这次的一个小问题! 翻出日志里的报错堆栈,找到对应的.java:LINE,从临时目录找到这个jsp对应的java文件,一看便知,以后碰到500都这么干。
+1
江城DiorsMan 2013-09-23
  • 打赏
  • 举报
回复
你可以改成这样,加判断,str1,str2,str3非空
  
     int a = 0;
     int b = 0;
     if(str1!=null){
   	  	 a=Integer.parseInt(str1);
     }
      if(str2!=null){
    	 b=Integer.parseInt(str2);
    }
    
     if(str3!=null){
      if(str3.equals("+")){
     out.print(a+str3+b+"="+(a+b));
     }
     if(str3.equals("-")){
     out.print(a+str3+b+"="+(a-b));
     }
    if(str3.equals("*")){
     out.print(a+str3+b+"="+(a*b));
     }
     if(str3.equals("/")){
     out.print(a+str3+b+"="+(a/b));
     }
     }
  • 打赏
  • 举报
回复
int a=Integer.parseInt(str1); int b=Integer.parseInt(str2); str1和str2转换之前最好先最下是否为""或null判断,如果不为空是不是你的str1或str2太长了?
zoeg 2013-09-22
  • 打赏
  • 举报
回复
代码就不看了,就是帮你找到问题,也只是解决这次的一个小问题! 翻出日志里的报错堆栈,找到对应的.java:LINE,从临时目录找到这个jsp对应的java文件,一看便知,以后碰到500都这么干。
失落夏天 2013-09-22
  • 打赏
  • 举报
回复
第一, int a=Integer.parseInt(str1); int b=Integer.parseInt(str2); 打印出来str1和str2看看 第二, if(str3.equals("/")){ out.print(a+str3+b+"="+(a/b)); } b为0怎么办?

81,092

社区成员

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

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