1

I have a jsp page which contains some fields. I need to validate those fields from server side (i.e,from java servlet class). I put all error messages in a map and I am forwarding the messages to the jsp page.

See my jsp page with css.

enter image description here

When I click on submit button the css is not working.

See this image

enter image description here

This is how I am forwaring my error messages to jsp page.

                if(errors.size()>0)
                        {
                         request.setAttribute("map",errors);
                         RequestDispatcher rd=request.getRequestDispatcher("admin/adduser.jsp");
                         rd.forward(request, response);

                       }

Please help me.

This css I added in head tag

This is my form code

Map<String,String> errors=null;
 %>
 <%
      if(request.getAttribute("map")!=null){
       errors=(Map<String,String>)request.getAttribute("map");
    }
    %>
    <body>
     <form action="../BrightSymphonyController" method="post">
      <%if(request.getParameter("name")== null ||"".equals(request.getParameter("name"))){ %>
        <input name="name" type="text" class="textbox2" maxlength=""/>
        <span style="color:red " class="error"><%=(errors!=null?(errors.get("name")!=null?errors.get("name"):""):"")%></span>
        <%} else{ %>
        <input name="name" type="text" class="textbox2" value="<%=request.getParameter("name")%>"/>
        <%} %>

        </form>
        </body>

I added only one field code.

Asha
  • 115
  • 1
  • 13

1 Answers1

0

As per your comment I've found that you've used relative url to link css in jsp.

<link href="../css/stylesheet.css" rel="stylesheet" type="text/css" />

So, when you forward your error message to adduser.jsp, it might happen that your browser will not find that intended css because you've given relative path like ../css/yourfile.css.

Instead use absolute url to your css using ${pageContext.request.contextPath} so no matter how you're accessing your page it'll always resolve to correct url path.

<link href="${pageContext.request.contextPath}/css/stylesheet.css" rel="stylesheet" type="text/css" />

Also read about ${pageContext.request.contextPath}

Community
  • 1
  • 1
Parth
  • 141
  • 1
  • 2
  • 14