SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Limit length of characters output in JSP string

    I have a JSP site reading data in from a bean. I want it to only print the first 40 characters of the description. How would I do this? Below is the code section reading in the data

    Code:
    <c:forEach items="${dresses}" var="bean" >
      <tr><td>${bean.name}</td><td>${bean.description}</td></tr>
    </c:forEach>

  2. #2
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found a JSTL function which can do this and have that working, however in the process I found the back button isn't working and I can't figure out why. If anyone can spot the issue, I'm sure it's something simple that I'm overlooking because I've been staring at this too long, I'd appreciate it.

    JSP code
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%-- imports the core tags from JSTL, prefix "c"--%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <%-- imports function tags from JSTL, prefix "fn"--%>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Bridesmaid Dresses</title>
        </head>
        <body>
            <table frame="box">
                <tr><th>Dress Name</th><th>Short Description</th></tr>
                <c:forEach items="${dresses}" var="bean" > 
                    <c:set var="shortDesc" value="${fn:substring(bean.description, 0, 40)}" /> 
                    <tr><td>${bean.name}</td><td>${shortDesc}</td></tr>
                </c:forEach> 
            </table>
            <br />
            <form action="Controller" method="POST" >
                <input type="submit" name="button" value="Back"> <%-- Return to the servlet --%>
            </form>
        </body>
    </html>
    Servlet
    Code:
    package homework;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.List;
    import java.util.Vector;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    public class Controller extends HttpServlet {
    
        private List<DressBean> dresses = new Vector<DressBean>(5);
    
        @Override
    public void init() throws ServletException {
            dresses.add(new DressBean("Alfred Angelo Pleated Strapless Dress",
                    "Who says you canít wear a bridesmaid dress again? This dress hugs your curves in all the right places to give a slimming look. The strapless sweetheart neckline is elegant and perfect for any bridesmaid. This tea length dress is fully lined dress, back zip, and dry clean only and is available in sizes 2 - 20W and comes in all 20 colors.",
                    149.99, "aa.jpg")); 
            dresses.add(new DressBean("David's Bridal Sleeveless Chiffon Dress",
                    "A 'must have' for your bridal party. This dress has a high neckline and keyhole detail on the back. Made from tafeta, this dress features an elastic waist to help flatter any figure. Designed at knee length it is fully lined with a back zip. This dress is available in canary, clover, malibu, and plum and is available in sizes 0 - 30W.",
                    149.0, "db.jpg")); 
            dresses.add(new DressBean("Jenny Yoo Organza Strapless Dress",
                    "A whimsical and charming look that your bridesmaids will love. This dress is breathtaking with the addition of pick-up points at the skirt. Made from organza, it has optional spaghetti straps to make everyone comfortable. This dress is available in all 20 colors and sizes 2 to 20W and 8JB to 16JB.",
                    159.99, "jy.jpg"));
            dresses.add(new DressBean("Lela Rose Chiffon Strapless Dress with Cap Sleeves",
                    "This ravishing dress features a sweetheart neckline with cap sleeves. Available in floor length or tea length, it includes an optional rhinestone flower waistband. It is available in wheat, silver mist, copper, sunset, and guava in sizes 0 to 20W and maternity sizes.",
                    140.99, "lr.jpg"));
            dresses.add(new DressBean("Donna Morgan Strapless Satin Ballgown",
                    "Take a step into the old world with the satin ballgown style dress. The illusion neckline and flowered waistband add a unique twist to this dress that everyone will love. Available in dark pacific, cameo, bermuda blue, and carnation and sizes sizes 0 to 30W and 8JB to 16JB.",
                    188.99, "dm.jpg"));
        }
    
        @Override
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            HttpSession session = request.getSession();
            String target = "/viewDresses.jsp"; 
            int index = 0; \
            Integer position = (Integer) session.getAttribute("position"); 
            if (position != null) 
            {
                index = position.intValue();
            }
            String button = request.getParameter("button");
            if (button == null || "".equals(button)) {
            } else if ("Next".equals(button)) {
                index = index + 1;
                if (index >= dresses.size()) {
                    index = 0; 
                }
            } else if ("Prev".equals(button)) {
                index = index - 1;
                if (index < 0) {
                    index = dresses.size() - 1; 
                }
            } else if ("List".equals(button)) {
                target = "/dressList.jsp";
                request.setAttribute("dresses", dresses);
            } else {
                return;
            }
            session.setAttribute("position", new Integer(index));
            request.setAttribute("dress", dresses.get(index));
            ServletContext context = this.getServletContext();
            RequestDispatcher dispatcher = context.getRequestDispatcher(target);
            dispatcher.forward(request, response);
        }
    
        @Override
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doGet(request, response);
        } 
    }

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,397
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    My guess is that your form action value is wrong. Did you mean "/Controller"

  4. #4
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,397
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Also, I don't know why you're doing this in servlet/jsp.... I strongly recommend that you use MVC framework. My fav is Spring MVC. No need to re-invent the wheel.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •