Now I am working with one of old site that was written using JSP. I see that everywhere in the code used
import ConnectionPool.ConnectionPool;
And I see that CLASSPATH set as:
classpath=%classpath%;C:\j2sdkee1.3\lib\j2ee.jar;C:\Working\Application\MyApp\Bean;
Could you please help understand me where I can search ConnectionPool.class?
May be in C:\Working\Application\MyApp\Bean or in my application directory?
Example of using ConnectionPool in JSP:
<code>
<%# page import="ConnectionPool.ConnectionPool;" %>
<%
application.removeAttribute("MYAPP_POOL");
ConnectionPool pool = new ConnectionPool();
pool.setDriver("sun.jdbc.odbc.JdbcOdbcDriver");
pool.setURL("jdbc:odbc:myapp");
pool.setUsername("Username");
pool.setPassword("Password");
pool.setSize(20);
pool.initializePool();
application.setAttribute("MYAPP_POOL",pool);
%>
</code>
Related
My aim is to import my class file in my index.jsp and reading over the internet I learned I'm supposed to use JavaBeans.
So here is my class file placed in WEB-INF/classes/jBean/Bucket.class -
package jBean;
import java.sql.* ;
import java.math.* ;
public class Bucket implements java.io.Serializable{
public static void main(String[] args){
Bucket obj = new Bucket();
obj.DBConn();
}
public static void DBConn(){
try{
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/studentfeedback","root","");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from student");
while(rs.next())
System.out.println(rs.getInt("Roll_No")+" "+rs.getString("Name")+" "+rs.getString("Pass"));
con.close();
}
catch(Exception e){
System.out.println(e);
}
}
And here is my index.jsp where I want to import the class file -
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<jsp:useBean id="jBean" class="jBean.Bucket" scope="session" />
</body>
</html>
Doing this I get this error message on executing index.jsp file
java.lang.ClassNotFoundException: org.apache.jsp.index_jsp
So, my query is how shall I import the class file, Bucket.java?
And is there any other way to import the file to index.jsp other than using javBeans?
Please help me with this as I can't find what I'm looking for. Maybe cause I might not be asking the right thing.
All I want to do is import the class file Bucket.java to the index.jsp
May God help the helping soul.
Did you observed your project folder structure.
The classes referenced by the jsp must be in the classpath.
And the classpath includes WEB-INF/classes.
The location of the jsp is of no importance.
question regarding jsp page import and <jsp:useBean id
New to EJB, please help:
To verify my EJB on local, I tried create a test.jsp that calls the EJB like this:
<%# page import="com.web.ejb.service.ContentInfo" %>
<% ContentInfo ci = ContentInfo.getContentById("123"); %>
When run the jsp, got error "Only a type can be imported. com.web.ejb.service.ContentInfo resolves to a package".
Then I replaced the import with
<jsp:useBean id="ContentInfo" class="com.web.ejb.service.ContentInfo" />
but got "ContentInfo cannot be resolved to a type."
Thanks for your help.
From the error message, seems like com.web.ejb.service.ContentInfo is not your class name. Maybe you made a typo ?
If you want to test an EJB more thoroughly, you can use OpenEJB to replicate the functionalities of an EJB server, in case of units tests for example.
First check whether the class name is correct and it is available in the above mentioned package. After that try this:
<%# page import="<package>.<YourBusinessInterface>, javax.naming.*"%>
<%!
try {
InitialContext ic = new InitialContext();
<YourBusinessInterface> obj = (<YourBusinessInterface>)
ic.lookup(<YourBusinessInterface>.class.getName());
} catch (Exception ex) {
// exception code here
}
%>
<html>
<body>
<% obj.callBusinessMethod(); %>
</body>
</html>
This link http://docs.oracle.com/javaee/5/tutorial/doc/bnbnp.html might help you. :)
<%# page import="java.util.*" %>
<html>
<body>
<h1 align="center">blablalblalblab</h1>
<p>
<%
List styles = (List)request.getAttribute("styles");
Iterator it = styles.iterator();
while(it.hasNext()) {
out.print("<br>try: " + it.next());
}
%>
</p>
</body>
</html>
after executing my servlet request i'm getting error
org.apache.jasper.JasperException: /result.jsp (line: 1, column: 18) quote symbol expected
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
can't find any quotes that are not on right place.
Make sure all your quotes are straight quotes, not curvy ones.
Don't use Java in JSPs, please. That's what the standard tag library is for.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<h1 align="center">blablalblalblab</h1>
<p>
<c:forEach items="${styles}" var="style">
<br>try: ${style}
</c:forEach>
</p>
</body>
</html>
In more detail:
Embedding Java code in a JSP makes the page difficult to read (JSP is a tag-oriented language), difficult to maintain, and difficult to debug.
The standard tag libraries are already debugged, have plentiful documentation and examples, and probably already Do What You Want To Do.
If you truly have some logic that needs to be performed in Java and no pre-existing tags exist, you can either a) put the logic in a bean and call it via JSTL or b) write your own tag using tagfiles.
Why is Java code better in a bean or tag library than in a JSP?
Testing is a big factor: beans and tag libraries can be tested
outside of a running servlet environment with ease.
Tag libraries are reusable and significantly cleaner than JSP includes.
I guess you have copy pasted it from somewhere, make sure the double quotes are proper. I had the same issue when I copied it from a PDF, it was resolved once I corrected my double quotes.
Your JSP works just fine with Tomcat 6. So, it's probably either some include-related issue or some previously compiled classes are not getting recompiled.
Try to clean up your Tomcat work directory and try again.
Check " if you copied it from somewhere. I had the same error because of ".
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%#page isELIgnored=”false” %>
double quotes around the false are the wrong ones. Change it to " and it will work.
While I agree with Scott A's admonition to use JSTL instead of putting Java directly into the JSP the question deserves being explored a little further. I just encountered this myself for the first time and had to dig a bit ot answer it.
Technically the error means what it says. You're missing some quotes somewhere. The simplest would be something like in your h1 tag if it read:
<h1 align=center>
instead of what you have.
<h1 align="center">
Obviously there is nothing in the code snippet that you pasted which is missing quotes so I would explore a couple of things.
First, what does the output of your it.next() look like? Since you're pulling in a list called styles I wonder if something in there is making jasper think it's a style tag instead of text you are trying to render.
Second, I would explore Pradeep's answer and see what if there is some subtle issue pasting that was resolved when you pasted it here on stackoverflow. Specifically I would look for 'smart quotes' IE many text editors (including outlook and most of office) like to use different quotes on the front and back of quoted text.
IE
'this is quoted text'
becomes
`this is quoted text'
which can be difficult to notice.
I'm new to JSP, using Eclipse, and am trying to just get started with templates. I've imported template.tld into WebContent/WEB-INF/tlds.
Guide: http://www.javaworld.com/jw-09-2000/jw-0915-jspweb.html
When I run the test.jsp file, I get this error:
org.apache.jasper.JasperException: /test.jsp(3,0) Unable to load tag handler class "tags.templates.InsertTag" for tag "template:insert"
I've tried searching Google, but am unable to find a solution. Any help would be greatly appreciated!
template.jsp
<%# taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %>
<html><head><title><template:get name='title'/></title></head>
<body>
Welcome!<br />
<template:get name='content'/>
</body></html>
test.jsp
<%# taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %>
<template:insert template='/template.jsp'>
<template:put name='content' content='this is our website'/>
</template:insert>
Have you also added the taglib's classes to /WEB-INF/lib or /WEB-INF/classes? Just having the TLD is not enough, you need the code that actually implements it. Your error message fragment suggests you haven't done that.
I've got a .jsp file that is working fine. It's maybe a bit special in that it is calling a factory (ArticlesFactory) that returns a singleton (but that is a detail) of class Articles (it does so by automatically fetching shared Google Docs that are transformed to html and then stored into ".../text/en" but that is a detail too).
The following is working fine: it does exactly what I need, it fetches the articles automatically and I can access my Articles instance fine.
<%# page pageEncoding="UTF-8" contentType="text/html;charset=utf-8" %>
<%# page import="com.domain.projectname.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head></head>
<body>
<% Articles articles = ArticlesFactory.create( getServletContext().getRealPath( "text/en" )); %>
We have <%= articles.getNbItems()%>
</body>
</html>
However, I must transform it to some notation I don't know nor understand, I'm not even sure what the name for that is and obviously I've got some issue.
I don't know if it's a namespace issue or if there's a problem with the ArticlesFactory factory's static factory method creating the Articles singleton:
<?xml version="1.0" encoding="UTF-8"?>
<jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:c="urn:jsptld:http://java.sun.com/jsp/jstl/core">
<jsp:directive.page import="com.domain.project.ArticlesFactory"/>
<jsp:directive.page contentType="text/html; charset=UTF-8" />
We have ${variable.nbItems} <!-- What to put here !? -->
</jsp:root>
I tried many things and couldn't figure it out.
Basically I need to:
- call the static create method from the ArticlesFactory class
- by passing it the result of getServletContext().getRealPath( "text/en" ))
(which should give back an Articles instance)
then I want to put the result of getNbItems() in a variable that I want to display
Note that I don't want to have to call getServletContext from any servlet/dispatcher: I want to do it just like in the first working example (ie directly from inside the .jsp).
You're basically looking for "JSP in XML syntax". Most is already explained in this (old) tutorial. You yet have to replace <% %> by <jsp:scriptlet> and <%= %> by <jsp:expression>.
The xmlns:c namespace is by the way unnecessary here, unless you'd like to use any of the JSTL core tags.
The Expression Language (those ${} things) which is explained in this (also old) tutorial is by the way a separate subject. It only acts on objects in the page, request, session or application scope. In scriptlets however, variables are only defined in local scopes (methodlocal actually), those aren't available in EL. You would need to do the following in the scriptlet to make it available in EL:
pageContext.setAttribute("articles", articles); // Put in page scope (recommended).
request.setAttribute("articles", articles); // Or in request scope. Also accessible by any include files.
session.setAttribute("articles", articles); // Or in session scope. Accessible by all requests in same session.
application.setAttribute("articles", articles); // Or in application scope. Accessible by all sessions.
This way it's available by ${articles} in EL.