Is it possible to get calling page name inside a jsp 2.0 custom tag?

I'm writing a custom JSP tag using the JSP 2 tag files. Inside my tag I would like to know which page called the tag in order to construct URLs. Is this possible with out passing it through an attribute?


Asked by: Wilson703 | Posted: 23-01-2022






Answer 1

Turns out that the request object actually is available, but only in the EL portion of a tag. So this would work:

<form action="${pageContext.request.requestURI}">

But not this:

<form action="<%=request.requestURI%>">

Or this:

<form action="<%=pageContext.request.requestURI%>">

Answered by: Elian621 | Posted: 24-02-2022



Answer 2

I think that within the tag code, you can examine the request object and its url, and determine the page from that.

Answered by: Ted775 | Posted: 24-02-2022



Answer 3

It is possible to access the request from within the tag file, via the pageContext member variable.

public class YourTag extends TagSupport {
    public int doStartTag() throws JspException {
        HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
        String pathInfo = req.getPathInfo();

Answered by: David371 | Posted: 24-02-2022



Answer 4

The request object is available in the tag. It doesn't matter if you use a class or a tag file. In tag files, it is available in Java scriptlets as well as in EL. However, it is available as a ServletRequest object and not an HttpServletRequest object (in EL the class of the object doesn't matter, but it does in scriptlets).

In addition, in your scriptlets you need to access the full method, not just a property name. So your code is supposed to be:

<form action="<%= pageContext.getRequest().getRequestURI() %>">

but even that won't work because getRequestURI() is a method of HttpServletRequest [1], not of ServletRequest. So either use EL, or use longer scriptlets in your tag file and cast the request object.

[1] http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServletRequest.html#getRequestURI()

Answered by: Chester837 | Posted: 24-02-2022



Similar questions

java - calling an exe file inside jar

I am trying to call the "dspdf.exe" inside the jar file where this smartpdf class exists. I plan to extract it to a temp location and delete when program ends. However this doesn't seem to work, any help will be appreciated. import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.omg.CORBA.portable.InputStream; public class smartpdf { static String url=""; static St...


java - Calling EJB from a WAR inside the same EAR

I am using Glassfish 4. And I have an EAR which has a WAR and a JAR (with the EJBs). I want to call the EJBs from my WAR but am not really sure if I need to use Local or Remote interfaces. Inside my JAR, my Bean looks like this : @Stateless public class Test implements TestLocal { @Override public void testing() { } } And my local :


java - calling class inside another class

Closed. This question needs details or clarity. It ...


java - Calling main inside the code

Can we call main() function inside any other function? I tried but did not come up with it. If we can't call it then why? Why main() is not like ordinary methods?


java - Code inside public static void main, or calling another method with code within main?

In tutorials on the internet I usually see pieces of code within public static void main(String[] args), whereas the lecturer for my programming course usually writes a method and calls that method within main. For example, in tutorials I'd see: class Person { public static void main(String[] args) { int age = 20; System.out.println(age); } } My...


java - How many JVM calling main inside main

class B { public static void main(String[] args) { } } class A { public static void main(String[] args) { B.main(args); } } In the above flow, my init method is A.main which in turn calls B.main. I know calling A.main will spawn a JVM. Does calling B.main inside A.main spawn another JVM? OR B.main is JUST another static method ...


c# - Best table / enum driven method calling system

Consider I'm interfacing with an external system that will send a message (DB table, message queue, web service) in some format. In the "message header" there is the "MessageType" that is a number from 1 to 20. The MessageType defines what to do with the rest of the message. There are things like new, modified, deleted, canceled... My first inclination was to setup an enumeration and define all the types. Then ...


Calling Java from Clojure

When I try to run the following code (from the REPL) in Clojure: (dotimes [i 5] (.start (Thread. (fn [] (Thread/sleep (rand 1000)) (println (format "Finished %d on %s" i (Thread/currentThread))))))) I get the following error: java.lang.Exception: Unable to resolve symbol: i in this context clojure.lang.Compiler$CompilerException: NO_SOURCE_FILE:6: Unabl...


Tail calling in Java and C#?

I was reading about Clojure and found a discussion about Java not supporting tail calls, in the current version, and that people were throwing exceptions to simulate tail calling in the JVM, anyways it sounds like people are doing some crazy stuff out there. So this got me wondering about C#'s tail calling, same issues?


java - Calling a Remote Bean vs Local Bean in App Server

Is there a noticeable amount of performance overhead in using Remote Bean Interface over using a Local Bean Interface? I would like to have every Client application connect to remote beans if there is little performance difference.


java - Calling a global Array

I'm currently trying to draw shapes with 2D Arrays. In my class there is a global array defined with public char canvas[][]; Up until now, I have only declared arrays with char canvas[][] = new char[height][width]; If this Array has already been declared, and I'm not supposed to amend the code I've been given, how do I call an instance of that array so that I can use it? th...


java - Calling servlet from another servlet

I have two servlets which are running on different tomcat servers. I and trying to call a servlet1 from servlet2 in the following way and wanted to write an object to output stream. URL url=new URL("http://msyserver/abc/servlet1"); URLConnection con=url.openConnection(); con.setDoOutput(true); con.setDoInput(true); OutputStream os=con.getOutputStream(); ObjectOutputStream oos=new ObjectOutputStream...


java - How do I find out the calling class?

how can i find out which class/method has called the actual method?


Unkown error when calling Java applet from JavaScript

Here's the JavaScript (on an aspx page): function WriteDocument(clientRef, system, branch, category, pdfXML) { AppletReturnValue = document.DocApplet.WriteDocument(clientRef, apmBROOMS, branch, category, pdfXML); if (AppletReturnValue.length &gt; 0) { document.getElementById('pdfData').value = ""; CallServer(AppletReturnValue,''); } PostBackAndDisplayPDF() }


java - calling an exe file inside jar

I am trying to call the "dspdf.exe" inside the jar file where this smartpdf class exists. I plan to extract it to a temp location and delete when program ends. However this doesn't seem to work, any help will be appreciated. import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.omg.CORBA.portable.InputStream; public class smartpdf { static String url=""; static St...


c# client calling java axis2 web service, object "resets"

I am very new to web service stuff so please be kind. I have written a simple POJO class, and deployed it on an axis2 server: public class Database { private Project project; public void login(){ project = new Project(); project.setDescription("Hello there"); project.setName("To me"); } public Project getProject(){ return project; } }






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top