0

I'm just starting out with Servlets, and i'm totally stumped.

I've looked at dozens of resources online, and tried them all without success. They all (with minor variations) have a jsp file which contains this code:

<%= request.getAttribute("message")%>

...and a servlet which contains this code:

String message = "Example source code of Servlet to JSP communication.";
    request.setAttribute("message", message);

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

I've put the above code in the doPost(), doGet(), and the body of servlet, nothing works. Then it occurred to me that there's no way the jsp file actually knows what servlet it's supposed to be communicating with. I've researched xml mapping, and annotation mapping, but they don't seem to apply.

So far I've communicated with Ajax to the servlet fine, but I can specify the action with ajax. How do I pass data from the jsp to the servlet, and then back again?

Any ideas would be appreciated!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mark Hill
  • 161
  • 2
  • 9

2 Answers2

2

Here is a simple Servlet-JSP communication sample code:

index.jsp

<!doctype html>
<html>
<head>
    <title>Servlet - JSP Communication Demo</title>
</head>

<body>
    <%
        if(request.getAttribute("message") != null) {
            out.println("<h1>" + request.getAttribute("message") + "</h1>");
        }
    %>

    <form method="get" action="sayhello">
        <label for="name">Enter your name</label>
        <input type="text" name="name" id="name" />
        <input type="submit" value="Submit">
    </form>
</body>
</html>

ServletDemo.java

package com.demo;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletDemo extends HttpServlet{

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if(request.getParameter("name") != null && request.getParameter("name") != "") {
            request.setAttribute("message", "Hello " + request.getParameter("name"));
        }else {
            request.setAttribute("message", "");
        }

        getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
    }
}

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>
        <servlet-name>Servlet-JSP Communication Demo</servlet-name>
        <servlet-class>com.demo.ServletDemo</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Servlet-JSP Communication Demo</servlet-name>
        <url-pattern>/sayhello</url-pattern>
    </servlet-mapping>
</web-app>

Directory Strecture

--tomcat
  --webapps
    --servlet
      --WEB-INF
        --classes
          --com
            --demo
              --ServletDemo.class
      --web.xml
    --index.jsp

Code Explanation

Here I have created a simple form that sends the form data to ServletDemo servlet through GET method. On the other hand the servlet gets the form data and replies back with some message. In JSP the message is displayed.

Kesavamoorthi
  • 959
  • 2
  • 11
  • 21
1

The approach you're taking is quite the ancient one! Here's a JavaWorld article from 1999 that describes how to communicate between the servlet and the JSP.

In the web.xml, you bind a URL path to your servlet. The app server will respond to HTTP GET on that URL path by calling doGet() on the servlet, and HTTP POST by calling doPost(). You can set attributes on the HttpServletRequest passed as a parameter to these methods. When you call the forward method with that request, the attributes will be shared with the JSP.

That's how it is supposed to work.

sh0rug0ru
  • 1,596
  • 9
  • 9