基础问题001
以下是书上的一段代码,但是我打了以后却没有出现该有的效果,请大家运行下,帮我看看错在哪里(我已经很仔细的同书上比对过了)
doget_form.html
- HTML code
<html><head><title>测试doGet</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><form action="/doget_servlet" method="get"><table><tr><td>输入名称后提交:<input type="text" name="name"></td></tr><tr><td><input type="submit" name="submit" value="确定"></td></tr></table></form></body></html>
web.xml
- XML code
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd" version="2.4"> <display-name>Welcome to Tomcat</display-name> <description> JSP </description> <servlet> <servlet-name>DoGetTestServlet</servlet-name> <servlet-class>classes.DoGetTestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DoGetTestServlet</servlet-name> <url-pattern>/doget_servlet</url-pattern> </servlet-mapping></web-app>
DoGetTestServlet
- Java code
public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginForm f = (LoginForm)form; if(ils.login(f.getUsername(), f.getPassword())) { request.setAttribute("fail", "恭喜您,登录成功!"); return mapping.findForward("index"); } else { request.setAttribute("fail", "对不起,登录失败!"); return mapping.findForward("login"); } }最终效果应该显示"获得了以下的参数: name=<br>" + request.getParameter("name")
但是运行后的页面时出错。
[解决办法]
<form action="/doget_servlet" method="get">
action要加上项目名如:/项目名/doget_servlet
[解决办法]
在用Eclipse创建jsp页面的时候,会自动生成一个绝对路径的地址。 BasePath....
所以如果在 jsp 页面中写 提交表单的 时候就不用加 : /项目名/Servlet地址。
而你是在html中创建的表单,他本身没有绝对路径,所以你得给它指定。
[解决办法]
朋友你的这个问题是这样子的:如果你使用MyEclipse开发工具 那么在你建立doget_form.html的时候 最好使用basic template 然后把<form action="/doget_servlet" method="get"> 这就话中的
action="/doget_servlet" 改为action="doget_servlet" 使用相对路径 注意 此时的doget_form.html文件也要放到web应用的根目录下.这样程序就能正常运行了.
你也可以将<form action="/doget_servlet" method="get"> 改为<form action="/csdn_path_problem/doget_servlet" method="get"> 这样也可以得出正确结果
[解决办法]
<form action="/doget_servlet" method="get">
<form action="doget_servlet" method="get">
[解决办法]
绝对路径和相对路径的问题。。。
<form action="/你的项目名/doget_servlet" method="get">
这样就可以了
刚学,具体怎么区分绝对路径和相对路径不是很清楚。。点击提交之后,注意看地址栏的url。