setAttribute 怎么用?

pandou 2004-06-15 08:33:09
我在struts中的action 用

httpServletRequest.setAttribute("list",list_out);
return actionMapping.findForward("ok");

跳转到 "ok" 所指向的页后,用
if(request.getParameter("list")!=null) out.print("ok");
可是就是显示不出来,请问各位前辈,我那个地方忘写了?
...全文
321 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnham 2004-09-25
  • 打赏
  • 举报
回复
mark
eaglecoody 2004-07-15
  • 打赏
  • 举报
回复
request.getParameter是获取html中对象的值
比如html中有<input type="text" name="mytext" value="abc" />
在servlet中用request.getParameter("mytext"),获取的应该是abc
request.getAttribute是获取setAttribute的数据对象!!!
wellshddelphi 2004-07-15
  • 打赏
  • 举报
回复
好东西我一定收藏...
bluesky35 2004-06-16
  • 打赏
  • 举报
回复
request.getParameter是获取html中对象的值
比如html中有<input type="text" name="mytext" value="abc" />
在servlet中用request.getParameter("mytext"),获取的应该是abc
request.getAttribute是获取setAttribute的数据对象!!!
interreal 2004-06-15
  • 打赏
  • 举报
回复
他的这个程序我试过了,结果出不来,与request写法没有关系,其原因就是我前面所说的他的配置文件写错了。
bluesky35 2004-06-15
  • 打赏
  • 举报
回复
一个servlet的例子:
package com.logon.src;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class logon extends HttpServlet {

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

HttpSession session = request.getSession();
String strname = request.getParameter("txtname");
String strmail = request.getParameter("txtmail");
String remove = request.getParameter("remove");
if(remove!=null && remove.equals("on")){
session.removeAttribute(strname);
}
else{
if(strname!=null && strname.length()!=0 && strmail!=null && strmail.length()!=0){
session.setAttribute(strname,strmail);
}
}

PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("welcome");
out.println("</head>");
out.println("<body>");
String url = "/useservlet/servlet/logon";
out.println("<form action=\"" + url + "\" " + "method=\"GET\">");
out.println("please input your name:<input type=\"text\" name=\"txtname\" />");
out.println("<br>");
out.println("please input your mail:<input type=\"text\" name=\"txtmail\" />");
out.println("<br>");
out.println("remove:<input type=\"checkbox\" name=\"remove\" />");
out.println("<br>");
out.println("<input type=\"submit\" value=\"Update\" />");
out.println("</form>");

out.println("<hr>");

Enumeration e= session.getAttributeNames();
while(e.hasMoreElements()){
String attr_name = (String)e.nextElement();
String attr_mail = (String)session.getAttribute(attr_name);
out.println("<br><b>Name:</b>");
out.println(attr_name);
out.println("<b>Value:</b>");
out.println(attr_mail);
}
out.println("</body>");
out.println("</html>");
out.close();
}
}
setAttribute要用getAttribute拿,而request.getParameter是拿页面上的值的,不要混淆.
fmzbj 2004-06-15
  • 打赏
  • 举报
回复
request.getAttribute
interreal 2004-06-15
  • 打赏
  • 举报
回复
你的程序没有问题,结果出不来的原因是你的配置文件错了。
<forward name="ok" path="ok.jsp" redirect="true" />中,应把redirect="true",写成:<forward name="ok" path="ok.jsp"/>。
redirect的默认值是false,如果设置成true,程序运行时将调用HttpServletResponse.sendRedirct()方法,而原本储存在HttpServletRequest中的值将丢失。所以你在ok.jsp页面调用request.setAttribute()时,结果出不来。
pandou 2004-06-15
  • 打赏
  • 举报
回复
/********************* struts-config.xml *********************/
<action-mappings>
<action input="test1.jsp"
name="testActionForm"
path="/testAction"
scope="request"
type="list.testAction"
validate="true">
<forward name="ok" path="ok.jsp" redirect="true" />
</action>
</action-mappings>
pandou 2004-06-15
  • 打赏
  • 举报
回复
我还是没有成功
请前辈指点
以下是我的程序
/*********************** action *************/
package list;

import org.apache.struts.action.*;
import javax.servlet.http.*;

public class testAction extends Action {
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
/**@todo: complete the business logic here, this is just a skeleton.*/
testActionForm testActionForm = (testActionForm) actionForm;
String list = "aaaaaaaaaaaaaaa";
httpServletRequest.setAttribute("list",list);
System.out.println(httpServletRequest.getParameter("pwd"));
String pwd =httpServletRequest.getParameter("pwd");
httpServletRequest.setAttribute("pwd",pwd);
list =(String) httpServletRequest.getAttribute("list");
System.out.println(list);
return actionMapping.findForward("ok");
}
}

/********************************* ok ****************** */
<%
if(request.getAttribute("list") != null) out.print("ok");
%>
/********************************* actionForm ****************** */
package list;

import org.apache.struts.action.*;
import javax.servlet.http.*;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class testActionForm extends ActionForm {
private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
/**@todo: finish this method, this is just the skeleton.*/
return null;
}
public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
}
}


sagittarius1979 2004-06-15
  • 打赏
  • 举报
回复
request.getParameter
->
request.getAttribute
HITZXL 2004-06-15
  • 打赏
  • 举报
回复
在struts中的action中 用
首先要public class ****Action extends Action{
然后要有这个这样的方法public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
在这个方法里,可以使用request.setAttribute(("list",list_out);

然后跳转到 "ok" 所指向的页后,用
if(request.getAttribute("list")!=null) out.print("ok");


一定不要用request.getParameter


pandou 2004-06-15
  • 打赏
  • 举报
回复
谢谢各位 的指点.

81,094

社区成员

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

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