Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Limit length of characters output in JSP string

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Thanked 0 Times in 0 Posts

    Default Limit length of characters output in JSP string

    I am outputting a string from a java bean and want to limit the number of characters. How can I do that? Here is the code I have in the JSP
    <table frame="box">
                <c:forEach items="${dresses}" var="bean" > <%-- Reads in the data in the dresses list in servlet--%>
                    <%-- Outputs the name and description of each item--%>

  2. #2
    Junior Member
    Join Date
    Oct 2013
    Thanked 0 Times in 0 Posts

    Default Re: Limit length of characters output in JSP string

    I found a JSTL function to do this and that is working exactly as desired, however in the process I found that the back button doesn't work. Everything looks fine that I can see, just loads a blank page each time though. I tried submitting via GET as well so it should load exactly as when the server is initially loaded, but it's not working either way. Below is the code

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    <%@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"--%>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Bridesmaid Dresses</title>
            <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)}" /> 
            <br />
            <form action="Controller" method="POST" >
                <input type="submit" name="button" value="Back">

    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);
    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"));
        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)) {
                // no button was pressed, so no action will be taken
            } 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 {
            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);
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doGet(request, response);

Similar Threads

  1. Average length of time for input/output
    By ManInTheMiddle in forum Threads
    Replies: 33
    Last Post: April 4th, 2013, 03:24 PM
  2. Replies: 2
    Last Post: March 28th, 2013, 09:54 AM
  3. Replies: 9
    Last Post: February 26th, 2013, 11:36 AM
  4. Reading characters from a simple output to list words.
    By adwodon in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 16th, 2012, 03:34 PM
  5. Maximum length of a string
    By ranjithfs1 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 6th, 2012, 09:47 AM