自己编了个《实现与客户端交互》的程序,但是

luojieone 2004-08-16 10:07:10
程序分为3部分:
1:处理POST:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SimpleServlet extends HttpServlet {

public void doGet( HttpServletRequest request,HttpServletResponse response )
throws ServletException,IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter( );
out.println("<HEAD><TITLE>ServletÊä³öʵÀý</TITLE></HEAD><BODY>");
out.println("<h1>Ò»¸öServletµÄ¼òµ¥Êä³öʵÀý</h1>");
out.println("</BODY>");
out.close( );

}

public String getServletInfo( ) {

return "输出实例";

}
}
2:HTML表单提交:
<html>
<head><title>Survey</title></head>
<body>
<from action=http://localhost:8080/kk/Survey method=POST>
<input type=hidden name=survey value=Survey01Results>

<br><br>你公司的人数:
<br>
<br>1-100<input type=radio name=employee value=1-10>
<br>100-200<input type=radio name=employee value=10-100>
<br>200-300<input type=radio name=employee value=100-1000>
<br>400-more<input type=radio name=employee value=1000-more>

<br><br>你公司的WEB开发语言是:
<br>
<br>ASP<input type=checkbox name=lang value=ASP>
<br>PHP<input type=checkbox name=lang value=PHP>
<br>JSP<input type=checkbox name=lang value=JSP>

<br><br>其它说明:
<br>
<br><input type=text name=comment>
<br>

<br><input type=submit><input type=reset>
</form>
</body>
</html>
...全文
85 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
luojieone 2004-08-16
  • 打赏
  • 举报
回复
以上的文件我存在TOMCAT的默认路径下,TOMCAT的配置没有问题、服务也在每次运行程序时候重启了的,1、2程序没有问题,出了结果,但是当我输入http://localhost:8080/kk/Survey看3个结果时候,出现以下错误:
1:type:Status report
2: message: HTTP method GET is not supported by this URL
3: description: The specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL).
请大家帮忙看看。


luojieone 2004-08-16
  • 打赏
  • 举报
回复
3:表单处理:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Survey extends HttpServlet {

public void doPost( HttpServletRequest request,HttpServletResponse response )
throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter toClient = response.getWriter( );
try{
String surveyName=request.getParameterValues("survey")[0];
FileWriter resultsFile = new FileWriter(System.getProperty("file.separator")
+ surveyName + ".txt", true );
PrintWriter toFile = new PrintWriter(resultsFile);
toFile.println("<BEGIN>");
Enumeration values = request.getParameterNames( );
while( values.hasMoreElements() ) {

String name = ( String )values.nextElement( );
String value = request.getParameterValues( name )[0];

if( name.compareTo("sumbit") != 0 ) {
toFile.println( name + ": " + value );

}

}

toFile.println("<END>");
resultsFile.close( );
toClient.println("<html>");
toClient.println("<title>Thank you</title>");
toClient.println("Thanks for copperation!");
toClient.println("</html>");

} catch( IOException e ) {

e.printStackTrace();
toClient.println("A problem occured while recording your answer." +
"Please try again.");

}

toClient.close( );

}

}

62,623

社区成员

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

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