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

没法从表单中把值传给servlet

2013-01-12 
无法从表单中把值传给servletform.htmform actionformServlet methodpost用户名:input typetex

无法从表单中把值传给servlet
form.htm
<form action="formServlet" method="post">
用户名:<input type="text" name="uname">
<input type="submit" value="提交">
</form>

FormServlet.java
package cn.mldn.lxh.servlet ;
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;

public class FormServlet extends HttpServlet
{
private ServletConfig config = null ;
public void init(ServletConfig config) throws ServletException 
{
this.config = config ;
}
// 表示处理get请求
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
this.doPost(req,resp) ;
}
// 处理post请求
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
String name = req.getParameter("uname") ;
// 取得application对象
ServletContext app = this.config.getServletContext() ;
app.setAttribute("addr","www.MLDN.cn") ;
// 取得一个session对象
HttpSession session = req.getSession() ;
session.setAttribute("sname",name) ;
 System.out.println("** Servlet doPost处理提交参数 ...") ;
System.out.println("name = "+name) ;
// 重定向
resp.sendRedirect("demo.jsp") ;
}
};

/*
  <servlet>
<servlet-name>form</servlet-name>
<servlet-class>cn.mldn.lxh.servlet.FormServlet</servlet-class>
  </servlet>
  <servlet-mapping>
<servlet-name>form</servlet-name>
<url-pattern>/formServlet</url-pattern>
  </servlet-mapping>
*/
demo.jsp
<h1><%=session.getAttribute("sname")%></h1>

<h1><%=getServletContext().getAttribute("addr")%></h1>
<h1><%=application.getAttribute("addr")%></h1>


tomcat已经启动
按提交以后地址栏转到:file:///C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%207.0/webapps/daima/formServlet servlet input session application
[解决办法]
好像是你的重定向有点问题吧,要不试试加上全的路径 
如:resp.sendRedirect("../../demo.jsp") ;

// 重定向
resp.sendRedirect("demo.jsp") ;
[解决办法]
你看看你form.htm页面上有没有下面这一段:如果没有加上
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

然后再修改你的form

<form action="<%=basePath%>formServlet" method="post">
用户名:<input type="text" name="uname">
<input type="submit" value="提交">
</form>

[解决办法]
你web.xml配置的没有问题 ,应该是Servlet这块代码有问题。
[解决办法]
我刚刚试了,你的代码可以的……
你看一下我的代码,我就是用的你的代码,配了一下环境,要不你用我的代码试试



index.jsp


<%@ 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="formServlet" method="post">
用户名:
<input type="text" name="uname">
<input type="submit" value="提交">
</form>
</body>
</html>


demo.jsp

<%@ 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 'demo.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>
<h1><%=session.getAttribute("sname")%></h1>
<h1><%=getServletContext().getAttribute("addr")%></h1>
<h1><%=application.getAttribute("addr")%></h1>
</body>
</html>


FormServlet .java

package com.csdn.test;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class FormServlet extends HttpServlet {

private ServletConfig config = null;

public void init(ServletConfig config) throws ServletException {
this.config = config;
}

// 表示处理get请求
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
this.doPost(req, resp);
}

// 处理post请求


public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
String name = req.getParameter("uname");
// 取得application对象
ServletContext app = this.config.getServletContext();
app.setAttribute("addr", "www.MLDN.cn");
// 取得一个session对象
HttpSession session = req.getSession();
session.setAttribute("sname", name);
System.out.println("** Servlet doPost处理提交参数 ...");
System.out.println("name = " + name);
// 重定向
resp.sendRedirect("demo.jsp");
}

}



web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>FormServlet</servlet-name>
    <servlet-class>com.csdn.test.FormServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>FormServlet</servlet-name>
    <url-pattern>/formServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>



[解决办法]
按提交以后地址栏转到:file:///C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%207.0/webapps/daima/formServlet 
说明重定向resp.sendRedirect("demo.jsp") 根本就没成功,因为重定向的时候,浏览器地址栏是会改变的
[解决办法]
file:///C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%207.0/webapps/daima/formServlet 
这种情形是不是servlet都没进去

如果是的话,直接访问formServlet行不行
http://127.0.0.1:8080/daima/formServlet 

热点排行