Questions tagged [scriptlet]

A Scriptlet is a piece of raw Java code embedded in a JSP file which gets executed in line with the JSP output.

A Scriptlet is a piece of raw Java code embedded in a JSP file which get executed in line with the JSP output (which is usually HTML). It's recognizeable by the Java code between the <% and %> tags. Its use is since JSP 2.0 (2003) as per the JSP coding conventions discouraged in favor of tag libraries like JSTL and expression language EL to control the flow of JSP output. The real business job such as preprocessing (e.g. loading data from DB) and postprocessing (e.g. saving submitted form) should be done in a servlet.

Scriptlets are these days considered old school and a bad practice. They are however still useful for quick prototyping and testing, for example to quickly prepare/stub values in top of JSP so that JSTL tags and EL expressions further down in the JSP page can quickly be prototyped/tested.

411 questions
1763
votes
31 answers

How can I avoid Java code in JSP files, using JSP 2?

I know that something like the following three lines <%= x+1 %> <%= request.getParameter("name") %> <%! counter++; %> is an old school way of coding and in JSP version 2 there exists a method to avoid Java code in JSP files. What are the…
chmoelders
  • 18,584
  • 6
  • 22
  • 24
55
votes
2 answers

I can pass a variable from a JSP scriptlet to JSTL but not from JSTL to a JSP scriptlet without an error

The following code causes an error: <% String resp = "abc"; resp = resp + test; pageContext.setAttribute("resp", resp); %> The error says "error a line 4: unknown symbol…
Cornish
  • 899
  • 2
  • 9
  • 8
38
votes
2 answers

Accessing a JSTL / EL variable inside a Scriptlet

The following code causes an error: <% String resp = "abc"; resp = resp + ${test}; //in this line I got an Exception. out.println(resp); %> Why can't I use the expression language "${test}" in the…
reddy
  • 667
  • 2
  • 9
  • 19
37
votes
2 answers

Are methods legal inside JSP scriptlet?

I know its not recommended, and I should be using tag libraries etc etc. But I'd still like to know if it is legal to declare methods in a JSP scriplet: <% public String doSomething(String param) { // } String test =…
bba
  • 14,621
  • 11
  • 29
  • 26
35
votes
7 answers

How to avoid using scriptlets in my JSP page?

I've been told that the use of scriptlets (<%= ... %>) in my JSP pages isn't such a great idea. Can someone with a bit more java/jsp experience please give me some pointers as to how to change this code so its more 'best practice', whatever that may…
Chris
  • 39,719
  • 45
  • 189
  • 235
30
votes
6 answers

Creating Array using JSTL or EL

I'm working on a web application using Java and its frameworks(Spring 3.1.1). And I'm trying to avoid using scriptlets as much as possible, however I can't find a way other than this to define an array: <% String[] alphabet = {"A", "B", "C", ...…
Alpha Carinae
  • 441
  • 2
  • 8
  • 11
27
votes
2 answers

How can i use JSTL variable in scriptlet?

I have to access the JSTL variable which is calculated inside the iterator. Excerpt of code: card: ${resultBean.cardNum} i would like to access…
Rakesh Juyal
  • 35,919
  • 68
  • 173
  • 214
21
votes
9 answers

How do I pass JavaScript values to Scriptlet in JSP?

Can anyone tell me how to pass JavaScript values to Scriptlet in JSP?
user601367
  • 2,308
  • 12
  • 34
  • 44
17
votes
1 answer

Access Model attribute in scriptlet

I am using Spring MVC, and in my Controller, I am setting a standard model attribute using: ... model.addAttribute("param", value); ... Now, I wish to access this in a scriptlet (within a JSP). For example: <% Object value = ***.get***("param");…
Saket
  • 45,521
  • 12
  • 59
  • 79
17
votes
1 answer

What is the difference between <%! %> and <% %> in JSP?

What is the difference between <%! %> and <% %> in JSP?
Scicare
  • 833
  • 5
  • 13
  • 26
14
votes
7 answers

JSP tags + scriptlet. How to enable scriptlet?

I have a page which uses a tag template. My web.xml is very basic. I simply want to run some code in the page. And no, I'm not interested in tags or other alternative. I want to use the bad-practice scriptlet haha. So far I'm getting this "HTTP…
Poni
  • 11,061
  • 25
  • 80
  • 121
14
votes
2 answers

iterating over Enum constants in JSP

I have an Enum like this package com.example; public enum CoverageEnum { COUNTRY, REGIONAL, COUNTY } I would like to iterate over these constants in JSP without using scriptlet code. I know I can do it with scriptlet code like…
Dónal
  • 185,044
  • 174
  • 569
  • 824
12
votes
1 answer

How to evaluate a scriptlet variable in EL?

I was wondering if there was anyway of using JSP in statement. E.g. So I want my JSP variable to checked against as well. Any suggestions? I have tried ignorantly just…
urema
  • 721
  • 4
  • 11
  • 22
10
votes
3 answers

Using if-else in JSP

I'm using the following code to print the name of the user on the browser:

Hello! I'm duke! What's you name?



     …
Awani
  • 394
  • 3
  • 7
  • 19
10
votes
2 answers

In JSP, how to identify the type of Object present in a List?

Is it possible in JSP to get the type of Object in List, just like we do in Java myDataBind.getResultsList().get(0).getClass(); or is it possible to achieve something like this: if ( myDataBind.getResultsList().get(0) instanceOf MyClass ) { …
Rakesh Juyal
  • 35,919
  • 68
  • 173
  • 214
1
2 3
27 28