1

rentForm.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="kz.edu.astanait.Item"%>
<%@ page import="java.util.Enumeration" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<% 

    Enumeration attr = request.getAttributeNames();
    int counter = 0;
    String name="";
        while(attr.hasMoreElements()){
            name = attr.nextElement().toString();
            if(name.contains("item")){
                Item item = (Item)request.getAttribute(name);
                counter++;
            }
        }
        Enumeration items = request.getAttributeNames();
        Item[] itemsArray = new Item[counter];
        int a = 0;
        while(items.hasMoreElements()){
            name = items.nextElement().toString();
            if(name.contains("item")){
                itemsArray[a] = (Item)request.getAttribute(name);
                a++;
            }
        }
%>

<form method="POST" action="Servlet">
    Your name<br>
    <input type="text" name="clientName"><br>
    Your email<br>
    <input type="email" name="clientEmail"><br>
    Items<br>
    <%
    for(int i = 0; i < itemsArray.length; i++){ 
    %>
    <input type="radio"
     name="clientItem"
     value="<%= itemsArray[i].getItemName() %>,
    <%= itemsArray[i].getItemPrice() %>">
    <%= itemsArray[i].getItemName() %>,
    <%= itemsArray[i].getItemPrice() %><br>
    
    <% } %>
    Comments<br>
    <textarea name="clientComment"></textarea>
    <input type="submit">

</form>

</body>
</html>

Servlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
        Item[] items = getItems();
        for(int a = 0; a<items.length; a++) {
            request.setAttribute("item"+(a+1), items[a]);
        }
        request.getRequestDispatcher("/jsp/rentForm.jsp").forward(request,response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
        
        String clientName = request.getParameter("clientName");
        String clientEmail = request.getParameter("clientEmail");
        String clientItem = request.getParameter("clientItem");
        String clientComment = request.getParameter("clientComment");
        
        request.setAttribute("clientEmail", clientEmail);
        request.setAttribute("clientItem", clientItem);
        String clientPrice = clientItem.substring(0, clientItem.indexOf(",")+1);
        request.setAttribute("clientPrice", clientPrice);
        request.setAttribute("clientComment", clientComment);
        request.setAttribute("clientName", clientName);
        
        
        request.getRequestDispatcher("/jsp/confirmation.jsp").forward(request, response);
    }

    protected Item[] getItems() {
        Item item1 = new Item();
        Item item2 = new Item();
        Item item3 = new Item();
        Item item4 = new Item();

        
        item1.setItemName("Apple");
        item2.setItemName("Android");
        item3.setItemName("Windows");

        item1.setItemPrice("500");
        item2.setItemPrice("530");
        item3.setItemPrice("550");
        
        Item[] items = new Item[3];
        items[0]=item1;
        items[1]=item2;
        items[2]=item3;
        return items;

I have such error when i'm running my server. I fill gaps in the form that pressing submit button and this message occurs.

Servlet.service() for servlet [kz.edu.astanait.Servlet] in context with path [/Lesson-2-web-app-hw] threw exception java.lang.IllegalStateException: Cannot forward after response has been committed
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:323)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312)
at kz.edu.astanait.Servlet.doPost(Servlet.java:58)

  • Your radio button have `name="itemName"` but in servlet you are getting it using `clientItem` change it and see. – Swati Sep 19 '20 at 13:57
  • @Swati, thanks, I changed but now the program shows error at kz.edu.astanait.Servlet.doPost(Servlet.java:58) – Damir Kadyrov Sep 19 '20 at 14:25
  • 1
    yes because of this line `doGet(request, response);` when `doPost` is called then that line get executed and you have written `request.getRequestDispa..` in `doGet` method and in `doPost` as well .Either change that or add the `doGet` code under `doPost` or create some function to do same. – Swati Sep 19 '20 at 15:26
  • @Swati thanks for the advice but it doesn't work – Damir Kadyrov Sep 19 '20 at 17:07
  • what changes you have made ? – Swati Sep 20 '20 at 06:34

0 Answers0