0

I want to pass some parameters from my java servlet to my html form to be shown. I have read some things about jsp but I was wondering if I can do it directly, using something like this :

<script> var temp = "${param['CookieServlet0.First_Name']}"; </script>
<input type = "text" name = "First_Name" id = "First_Name" value = "temp" > <br>

where First_Name is a parameter of my servlet CookieServlet0. I know that the code example is wrong, is there a way to fix it so that I won't have to use jsp?

EDIT : ok since there is no way around I am starting with jsp, I've checked what you guys sent about JSTL and I do not need the extras that it offers so I am trying to keep it simple since I am just starting. So I have written the code below but I get java.lang.NullPointerException. It must be something really obvious but I cannot see what I do wrong...all the tutorials I have seen use the exact same commands...

java servlet :

 public void doGet( HttpServletRequest request,        // reaction to the reception of GET
                  HttpServletResponse response )
                  throws ServletException, IOException
   {

    String First_Name = request.getParameter("First_Name");
    String Last_Name = request.getParameter("Last_Name");
    String Phone_Number = request.getParameter("Phone_Number");
    String Address = request.getParameter("Address");

    PrintWriter output;
    Cookie cookies[];

    cookies = request.getCookies(); // get client's cookies

    if ( cookies.length != 0 ) {                                               
        String departure = cookies[0].getValue();
        String destination = cookies[1].getValue();

    }

    request.setAttribute("First_Name",First_Name);
    String strViewPage="formB.jsp";
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(strViewPage);
    dispatcher.forward(request, response);
 }

formB.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<label for = "First Name"> First Name </label>
<input type = "text" name = "First_Name" id = "First1_Name" value = "${First_Name}" 
"> <br>
</body>
</html>
Saraki
  • 297
  • 1
  • 7
  • 21
  • How do you render your HTML form without using JSP? You just have a static HTML file and unrelated servlet? – dimoniy May 20 '14 at 19:50
  • @dimoniy you can even work with plain HTML to send the data to a servlet. JSP acts more as view technology to display and render the data retrieved from Servlet. – Luiggi Mendoza May 20 '14 at 19:53

2 Answers2

0

No, you can't do it directly, unless you write your own parser/injector. However, using beans gets as close as you can get. Just use the <jsp:useBean> and replace the html with the values of the bean's attributes.

A quick google search yielded this site which contains examples on how to use the beans and jsp: http://www.roseindia.net/jsp/simple-jsp-example/UsingBeanScopeApplication.shtml

If you want to use JSTL, as Luiggi mentioned, here is a good website: http://www.journaldev.com/2090/jstl-tutorial-with-examples-jstl-core-tags

Alexandre Santos
  • 8,170
  • 10
  • 42
  • 64
0

Try Using:

RequestDispatcher dispatcher = request.getRequestDispatcher(strViewPage);

Instead of:

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(strViewPage);
Unheilig
  • 16,196
  • 193
  • 68
  • 98