首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

jsp+servlet的有关问题

2012-01-23 
jsp+servlet的问题我用的是eclipse3.0.1+tomcat5+lomboz3.0,我写了一个index.jsp和一个servlet:我在jsp中

jsp+servlet的问题
我用的是eclipse3.0.1+tomcat5+lomboz3.0,我写了一个index.jsp和一个servlet:
我在jsp中使submit提交到servlet上去,可是我的页面刚打开就打不开。
不知道是不是配置有问题,代码如下:
jsp如下:
<!-- Copyright (c) 2002 by ObjectLearn. All Rights Reserved. -->
<html>
<head>
<title>FormGetExample</title>
</head>
<body background="#FFFFF">
  <form name="form" method="get" action="http://localhost:8080/servlet/FormGetExample1">
  <table width="261" height="30">
  <tr>
  <td width="109" height="10">姓名:</td>
  <td width="276" height="10">
  <input type="text" name="nickname">
  </td>
  </tr>
  <tr>
  <td width="109" height="8">密码:</td>
  <td width="276" height="4">
  <input type="password" name="pssword1" value="">
  </td>
  </tr>
  </table>
  <p align="center">
  <input type="submit" name="submit" value="确定">
  <input type="submit" name="submit" value="重来">
  </p>
  </form>
  </body>
</html>

servlet如下:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class FormGetExample1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>Servlet Get Example</TITLE></HEAD>");
out.println(" <BODY>");
out.print("<p>&nbsp;</p>");
out.print("<p><font size='+4'>"+"Servlet Get Example"+"</font></p>");
Enumeration Enumeration = request.getParameterNames();
while(Enumeration.hasMoreElements())
{
String key = (String)Enumeration.nextElement();
String value = request.getParameter(key);
out.print("<P>");
out.print(key+"="+value);
out.print("</P>");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
web.xml如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Copyright (c) 2002 by ObjectLearn. All Rights Reserved. -->
<web-app>
<servlet>
<servlet-name>FormGetExample1</servlet-name>
<display-name>FormGetExample1</display-name>
<description>FormGetExample1</description>
<servlet-class>FormGetExample1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FormGetExample1</servlet-name>
<url-pattern>FormGetExample1</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>

</web-app>


请高手指教,给高分。


[解决办法]
估计是URL的问题吧。。
你项目名是什么??servlet???
http://localhost:8080/项目名/FormGetExample1

[解决办法]
action应该写FormGetExample1吧

[解决办法]
action写的不对,应该为
action="FormGetExample1"
不行就试试
action="/FormGetExample1"
action="./FormGetExample1"


[解决办法]
<url-pattern>/FormGetExample1 </url-pattern>
[解决办法]
首先最好不要把这么名字搞成一样,容易混掉 ,把下面两个地方改下试试

HTML code
<form name="form" method="get" action="FormGetExample1"> <input type="submit" name="submit" value="确定"> <input type="reset" name="reset" value="重来">
[解决办法]
<servlet> 
<servlet-name>FormGetExample1 </servlet-name> 
<display-name>FormGetExample1 </display-name> 
<description>FormGetExample1 </description> 
<servlet-class>这里应该把FormGetExample1所在包名写完整 </servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>FormGetExample1 </servlet-name> 
<url-pattern>这里必须有/FormGetExample1 </url-pattern> 
</servlet-mapping> 


还有页面FORM中的ACTION="这里应该写配置文件中的<url-pattern>中的地址:/FormGetExample1 "
[解决办法]
探讨
<url-pattern>/FormGetExample1 </url-pattern>

[解决办法]
<servlet> 
//这里名字要与mapping的名字对应
<servlet-name>FormGetExample1 </servlet-name> 
<display-name>FormGetExample1 </display-name> 
<description>FormGetExample1 </description> 
//这里是FormGetExample1这个类的路径,也要包括包的名字
<servlet-class>FormGetExample1 </servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>FormGetExample1 </servlet-name> 
这里是JSP页面的引用路径
<url-pattern>/FormGetExample1 </url-pattern> 
</servlet-mapping> 


action="http://localhost:8080/servlet/FormGetExample1" 
改为 
action="/FormGetExample1"
这样检查下应该就可以了。
[解决办法]
探讨
action写的不对,应该为
action="FormGetExample1"
不行就试试
action="/FormGetExample1"
action="./FormGetExample1"


热点排行