简体   繁体   中英

Not able to pass value from servlet to JSP

Code Updated with web.xml and Servlet configuration

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.

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:

package user;

    public class CompileClass {
    public String name;

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

    public String getName(){
            return name;
        }
    }

Here is my 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:

<%@ 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:

<?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. Help?

How is the value from the form getting into your 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. 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:

You aren't deploying your servlet in web.xml . 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. 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类...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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