简体   繁体   English

无法将值从servlet传递到JSP

[英]Not able to pass value from servlet to JSP

Code Updated with web.xml and Servlet configuration 使用web.xml和Servlet配置更新代码

I am trying to forward some value from my servlet to JSP, but when I am trying to access that value into JSP, there is no output. 我试图将某些值从servlet转发到JSP,但是当我尝试将该值访问JSP时,没有输出。

Here is my form: 这是我的表格:

<HTML>
<BODY>
<FORM METHOD=POST ACTION="servlet/NewServlet">
Enter Name: <Input type="text" name="name"/><br>
<P><INPUT TYPE=SUBMIT>
</FORM>

Here is my Bean I am using for setter and getter methods: 这是我用于setter和getter方法的Bean:

package user;

    public class CompileClass {
    public String name;

    public void setName(String n){
            name=n;
        }

    public String getName(){
            return name;
        }
    }

Here is my servlet: 这是我的servlet:

import java.io.*;
import javax.servlet.*;  
import javax.servlet.http.*;

import user.CompileClass;

public class NewServlet extends HttpServlet{

    public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String name1;

CompileClass c=new CompileClass();
name1=c.getName();
request.getSession().setAttribute("name1", name1);

request.getRequestDispatcher("../Result.jsp").forward(request, response);

    }
    }

And finally here is my JSP page: 最后是我的JSP页面:

<%@ page import="java.net.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="java.util.ArrayList"%>

<jsp:useBean id="user" scope="request" class="user.CompileClass" />
<jsp:setProperty property="*" name="user"/>
<html>

  <body>

Name:
<br/>

<% request.getSession().getAttribute("name1");%>


  </body>
</html>

This is my web.xml: 这是我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    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 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>NewServlet</servlet-name>
    <servlet-class>NewServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>NewServlet</servlet-name>
    <url-pattern>/servlet/NewServlet</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Everything seems fine to me. 在我看来一切都很好。 But still I am not getting any output on my JSP page. 但是我的JSP页面上仍然没有任何输出。 Help? 救命?

How is the value from the form getting into your Bean? 表单中的值如何进入您的Bean? You are doing: 您正在执行:

CompileClass c=new CompileClass();      //creates a new, empty instance
name1=c.getName();                      //gets the name from the empty 
                                        //instance (will be null)
request.setAttribute("name1", name1);   //sets 'name1' to null in the request

You might have better luck if you do: 如果您这样做,可能会带来更好的运气:

CompileClass c=new CompileClass();      //creates a new, empty instance
c.setName(request.getParameter("name"));//get the param and set it in the bean
name1=c.getName();                      //gets the name from the bean
request.setAttribute("name1", name1);   //sets 'name1' in the request

Although from the structure of your code, it seems like you are probably using some web framework that you expect to populate the Bean automatically for you with data from the request. 尽管从代码的结构来看,您似乎正在使用某种Web框架,希望该框架使用请求中的数据自动为您填充Bean。 If so, then you may want to consult the configuration and usage docs for your framework to make sure that you have it set up correctly and that you are using it correctly. 如果是这样,那么您可能要查阅框架的配置和使用文档,以确保已正确设置框架并正确使用它。

Edit: 编辑:

Also, please do not do things like: 另外,请勿执行以下操作:

<% request.getSession().getAttribute("name1");%>

Use this instead: 使用此代替:

${name1}

Edit 2: 编辑2:

You aren't deploying your servlet in web.xml . 您不会在web.xml部署servlet。 You need to configure it by adding something like: 您需要通过添加以下内容进行配置:

<servlet>
  <servlet-name>newServlet</servlet-name>
  <servlet-class>user.NewServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>newServlet</servlet-name>
  <url-pattern>/name/*</url-pattern>
</servlet-mapping>

And then you need to update your form HTML so that the form posts to the servlet instead of directly to the JSP. 然后,您需要更新表单HTML,以使表单发布到servlet,而不是直接发布到JSP。 So something like: 所以像这样:

<FORM METHOD=POST ACTION="name/submit">

It seems, like trouble with scopes. 似乎像合并示波器一样麻烦。 The easiest way to fix it is replace 修复它的最简单方法是替换

request.setAttribute("name1", name1);

to

request.getSession().setAttribute("name1", name1);

使用RequestDispatcher类...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM