完整代码为:<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
posttoself
</title>
</head>
<body bgcolor="#ffffff">
<h1>Post to self test</h1>
<form method="post" >
<br>请输入一个数字 : <input name="num"><br>
<input type="submit" name="Submit" value="提交" >
<input type="reset" value="清除">
<br>你输入的数字是<%=request.getParameter("num")%>
<%
if (request.getParameter("num")=="22")
{out.write("恰好是你的年龄哦:)");
}
%>
<br>
</form>
</body>
</html>
<html>
<head>
<title>
posttoself
</title>
</head>
<body bgcolor="#ffffff">
<h1>Post to self test</h1>
<form method="post" >
<br>请输入一个数字 : <input name="num"><br>
<input type="submit" name="Submit" value="提交" >
<input type="reset" value="清除">
<br>你输入的数字是<%=request.getParameter("num")%>
<%
if (request.getParameter("num")=="22")
{out.write("恰好是你的年龄哦:)");
}
%>
<br>
</form>
</body>
</html>
if (request.getParameter("num").equals("22"))
{out.write("恰好是你的年龄哦:)");
}
%>
{
if (request.getParameter("num").equals("22"))
{out.write("恰好是你的年龄哦:)");
}
}最好写成这样,不然的话,第一次运行时,由于request.getParameter("num")==null,会导致
request.getParameter("num").equals("22")出错.
primitives用==比较
if (request.getParameter("num").equals("22"))
{%>
<H1><%="恰好是你的年龄哦:)");"%></H1>
<%
}
%>
if (request.getParameter("num").equals("22"))
{
out.write("恰好是你的年龄哦:)");
}
%>
地址栏输入:http://localhost:8080/gb/posttoself.jsp
为空的时候出现下面的错误: The server encountered an internal error () that prevented it from fulfilling this request.但是即使http://localhost:8080/gb/posttoself.jsp?num=22
第一次显示出"
恰好是你的年龄哦:)"
之后,
再输入其它字符,
这句话仍然输出了出来:(我,我快郁闷死了,就前两天才出的错误:(
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
posttoself
</title>
</head>
<body bgcolor="#ffffff">
<%
if(request.getParameter("num")=="22")
{
out.write("恰好是你的年龄哦:)");
}
%>
<h1>Post to self test</h1>
<form method="post" action="yourJspPath">
<br>
请输入一个数字 : <input name="num">
<br>
<input type="submit" name="Submit" value="提交" >
<input type="reset" value="清除">
<br>
</form>
</body>
</html>
又测试一下,突然想起,改了一下,把<form method="post" >改成<form method="post" action="posttoself.jsp">就好了,原来假如我通过地址栏输入http://localhost:8080/gb/posttoself.jsp?num=22来打开页面的话,
在<form method="post" >的时候,它始终都是提交的num=22,5555555555555555555555555真是郁闷无比啊:(对了问大家一下,
在用到
if request.getParameter("num")的时候,都必须先判断
if (request.getParameter("num")!=null)吗???
<html>
<head>
<title>
posttoself
</title>
</head>
<body bgcolor="#ffffff">
<h1>Post to self test</h1>
<form method="post" >
<br>请输入一个数字 : <input name="num"><br>
<input type="submit" name="Submit" value="提交" >
<input type="reset" value="清除">
<br>你输入的数字是<%=request.getParameter("num")%>
<%
if (request.getParameter("num")!=null)
{
if (request.getParameter("num").equals("22"))
{out.write("恰好是你的年龄哦:)");
}
}
%>
<br>
</form>
</body>
</html>