Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing url parameter using jstl

Tags:

jsp

servlets

jstl

I am new to jstl and I need help getting a url-string parameter on a jsp that also contains EL markup from an iterated list of objects retrieved from a database. Can someone show me how to fix the code below so that the following line of code populates with an actual number where I am asking for ${param.spid}:

<a href="create-course-summary?spid="${param.spid}>add</a>

Here is the background:

I am calling a servlet with the following url pattern:

view-course-summaries?spid=1  

This calls the following doGet method in a servlet:

protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
   throws ServletException, IOException {
    String spidString = req.getParameter("spid");
    Long spid = new Long(spidString);
    List<CourseSummary> coursesummaries = new CourseSummaryDAO().findAllCS(spid);
    req.setAttribute("coursesummaries", coursesummaries);
    jsp.forward(req, resp);
}

And returns the following jsp:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ include file="admintop.inc" %>
<table>
<tr>
  <td>Name of School (Course Provider):</td>
  <td>will go here</td>
</tr>
<tr><td colspan=2>
<a href="create-course-summary?spid="${param.spid}>add</a>
</td>
</tr>
<tr>
   <td colspan=2>
        <table>
            <tr>
                <th>Type</th>
                <th>Number</th>
                <th>id</th>
            </tr>
            <c:forEach varStatus="loopCounter" items="${coursesummaries}" var="coursesummary">
            <tr>
                <td>
                    <c:out value="${coursesummary.coursetype}" />
                </td>
                <td>
                    <c:out value="${coursesummary.numunits}" />
                </td>
                <td>
                    <c:out value="${coursesummary.id}" />
                </td>
            </tr>
            </c:forEach>
        </table>
   </td>
</tr>
</table>   

<%@ include file="adminbottom.inc" %>
like image 366
CodeMed Avatar asked Aug 10 '13 19:08

CodeMed


People also ask

Can we use JSTL in HTML?

No. It is not possible to have JSTL in HTML page.

Can we use JSTL in JavaScript?

Java Using JSTLThis allows you to mix HTML, JavaScript, and Java in the same file. Therefore, it is reasonable to expect that at some point we will want to localize a string from within the Java code.

What is the alternative for JSTL?

Thymeleaf, JSF, AngularJS, JavaScript, and guava are the most popular alternatives and competitors to JSTL.

What is difference between JSTL and JSP?

JSP lets you even define your own tags (you must write the code that actually implement the logic of those tags in Java). JSTL is just a standard tag library provided by Sun (well, now Oracle) to carry out common tasks (such as looping, formatting, etc.).


1 Answers

Try this

<a href='create-course-summary?spid=${param["spid"]}'>add</a>
like image 166
superEb Avatar answered Sep 25 '22 06:09

superEb