I have a jsp file were the db connection is created and queries can be executed.
I want to call these functions (of connecting to a database and executing query) from another jsp simply to add new record to a database and to stay on a same page.
I read here about an "import" tag but i don't have any packages. Also i'm not allowed to use JSTL.
Little help?
Thanks for considering my question.
You can use <%#include %> directive to statically include JSP fragments.
<%#include file="db.jsp" %>
Writing JSPs this way is however considered poor practice. Java code belongs in a Java class, not in a JSP file. You could for example use a preprocessing servlet class wherein you do the job in doGet() method and have a reuseable DAO class wherein you hide all the JDBC boilerplate code away.
You shouldn't program in your jsp. Put that connection logic out into a class and use that in your jsps.
Afterwards you can import your class with:
<%# page import="package.yourclass" %>
In general you should consider using frameworks like Spring MVC or Struts. Using JSF could be another option.
Mainly we used to import two types of pages into a jsp page,
Java pages
JSP pages
If you want to import a java page into your jsp page, syntax is
<%# page import="package_name.java_page" %>
If you want to import a jsp page, syntax is
<%# include file='jsp_page_name.jsp'%>
while adding a jsp page, if it is in an another folder, you just need to specify the paths also.
Related
I have two jsp files (header.jspf and footer.jspf) that I want to include in a page produced by the doGet method of a servlet (Menu.java). I'm doing this by use of the RequestDispatcher.include() method. The header.jspf file only contains html so it renders fine. The footer.jspf file, however, contains both EL and JSP tags, neither of which render on the servlet page produced by Menu.java.
Looking at the source code of the page produced by Menu.java I understand that the reason for this problem is that the EL and JSP is not evaluated prior to being included in the servlet so it is just presumed by the browser that it is html.
While I'm guessing that what I'm trying to do may be poor (and deprecated) practice, I'd just like to find out if there is a way to get an included jsp file to render in a servlet just as it would in a jsp page when it contains EL and jsp tags?
I'm thinking that this question is general enough not to require my code to be posted, however if I'm wrong please tell me so and I will update with my code.
Not sure if there is a way to have RequestDispatcher.include() files processed, but including the footer explicitly will very likely work:
<%#include file="footer.jspf" %>
Why I have this error in my JSP file
My JSP:
Error Message:
Syntax error, insert "}" to complete MethodBody
A JSP has been parsed incorrectly by Eclipse. It has mistreated curly braces used by some javascript code that was rendered without <script> tag.
The <script> tag should be placed inside the <head> or <body> tags.
JSP files are compiled to a servlet. If you declare a method inside the JSP page using scriptlets, the method body is opened with { and should be closed with }, but somwhere in the code you might find /*}*/, or even worse missing <%}%>. The example of such errors (redundant }; in the Java code) you can find here.
To resolve this and other possible errors caused by spaghetti code inside the JSP, recommended way is to not use scriptlets and move Java code to a servlet. Struts is MVC framework that provided additional to servlets features for rendering JSP pages. You have to move Java code from JSP page to Struts controllers and access it by expression languages like JSTL, OGNL, etc. Return JSP page as a result/forward of the action/controller execution/ method call. You can also call methods of the model/controller directly while the page is rendered. This behaviour is out of the scope in which MVC pattern is used.
If you need more information about separation of concerns while developing a web application, particularly related to how to avoid Java code in JSP see How to avoid Java code in JSP files.
So there seems to be a few ways to include jsp files in jsp files, being:
<%# include file="header.jsp" %>
<jsp:include page="header.jsp" />
<c:import url="header.jsp" />
<tagfiles:tagfile />
So which one should I use and why? What advantages / disadvantages do they come with?
The include directive, makes a copy of the included page and copies it into a JSP page (the "including page") during translation. This is known as a static include (or translate-time include) and uses the following syntax:
<%# include file="/jsp/userinfopage.jsp" %>
Two alternatives exist for the dynamic include
The jsp:include tag, described in "Standard Actions: JSP Tags", dynamically includes output from the included page within the output of the including page during execution. This is known as a dynamic include (or runtime include) and uses the following syntax:
<jsp:include page="/jsp/userinfopage.jsp" flush="true" />
<c:import url="header.jsp" />
Unlike jsp:include, the c:import action provides a mechanism to access resources that can be specified through a URL, thus allowing page authors to get access to resources that reside outside the Web application. On the other hand, it lacks the ability to flush the response. Finally, c:import is comparatively more heavyweight and is therefore not appropriate when a lightweight solution is sought.
tagfiles are basically templates, which are like generic and can render some common views, but internally they will themselves use html tags itself.but not much of use while including jsp pages.
In my Project, I have used following approach
<jsp:include page="header.jsp" />
I have used this for loading specific div element instead of refreshing whole page.
This can be done by using JQuery's load method.
Including JSP file allows us to reuse the template in many places. Just write template code in JSP file and use it wherever required.
JSP page directives works at translation time while standerd actions works at run time.
You can tagfiles for calling functions on server side. You can also use tagfiles for creation of templates.
I'm building a taglib with a few custom tags. Some of these require a bit of JavaScript to work properly. What would be the best way to add the JavaScript so that it's included in the taglib jar?
The most obvious approach to me would be to put it into the custom tags' doStartTag() method like this pageContext.getOut().print("<script>my js</script>");, which I don't like for 2 reasons: It looks absolutely horrible and it gets added every time the tag gets used on a page.
I'm looking for a solution that lets me keep the JavaScript in a separate .js file that rests somewhere in the taglib jar and gets included only once per page whenever a custom tag is used.
Is it possible to inherit taglibs or imports from parent JSPs in their "descendants"?
Let me show you an example
header.jsp
<%# page contentType="text/html" isELIgnored="false"
import="org.something.utils.Constants"%>
//some code, Constants class is available here
index.jsp
<jsp:include page="template/header.jsp" />
//Constants is not available, I get a JasperException: Unable to compile class for JSP
also the taglibs inheritance doesn't seem to work. So is there a way how to make this work?
Taglibs and imports are not inherited, and everything in a tag as well cannot be inherited or passed through pages (except for JspContext and request attributes).
You have two options here:
Make the import in every JSP you have.
Make the common classes and libraries Global ones, this depends on the IDE and the server you are running.
Edit
Defining JSP implicit includes:
For Netbeans http://docs.oracle.com/cd/E19575-01/819-3669/bnajl/index.html
A global tutorial http://sabahmyrsh.blogspot.com/2009/06/jsp-defining-implicit-includes.html