jsp - whats wrong with my import of a jar file? - java

I am trying to integrate Simplify Commerce payment system into my project
but I can't seem to get it right.
This is one of the problems:
<%# page import="com.simplify.package"%>
and I got this error after mousing over the red wavy line:
Syntax error on token "package", * expected
I then added a `*` like this `com.simplify.package.*`
And I got this:
`Syntax error on token "package", Identifier expected`
I already added all the .jar files to the `WEB-INF/lib` folder.
What seems to be the problem?
The jsp that I am trying to import the jar file:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page import="com.simplify.package.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Pay Now!</title>
</head>
<body>
<%
PaymentsApi.PUBLIC_KEY = "";
PaymentsApi.PRIVATE_KEY = "";
CardToken cardToken = CardToken.create(new PaymentsMap()
.set("card.addressCity", "OFallon")
.set("card.addressState", "MO")
.set("card.cvc", "123")
.set("card.expMonth", 11)
.set("card.expYear", 19)
.set("card.number", "5105105105105100")
);
System.out.println(cardToken);
%>
There are red way lines under PaymentsApi and CardToken

Unpacking the jar file to look at the package structure solved it.

Related

What is the difference between <html> and <html:html> element in JSP?

I am developing a simple Struts 1.x web application and there's a file named success.jsp and this is the sample code:
<%# taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%# taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%# taglib uri="http://struts.apache.org/tags-nested" prefix="nested"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>success.jsp</title>
<html:base/>
</head>
<body>
Go to myStart
</body>
</html:html>
By default, <html>was used instead of <html:html>, may I know what is the major difference between these two elements? Is it necessary to specify the uses of them? Besides, what is the major function for <html:base/> element?
Btw I found some definitions for these elements but I need clarification:
<html:html> Renders an HTML <html> element with language attributes extracted from the user's current Locale object, if there is one.
<html:base> Renders an HTML element with an href attribute pointing to the absolute location of the enclosing JSP page. This tag is valid only when nested inside an HTML <head> element. This tag is useful because it allows you to use relative URL references in the page that are calculated based on the URL of the page itself, rather than the URL to which the most recent submit took place (which is where the browser would normally resolve relative references against).
The <html:html> tag is a Struts 1.x JSP Taglib directive, declared in this line on your JSP Page:
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
These custom tag(s) are typically of the form <prefix:tagname>. The prefix declared on taglib is what binds your taglib container to the list of markups available in the taglib.
In essence <html:html> tells the taglib, prefixed html to render a html element when JSP is rendered.
So to answer your question <html> is a HTML directive while <html:html> is a Struts JSP taglib tag to generate a HTML <html> directive.

null request response in simple jsp file

I am trying to just set up a very basic jsp file that takes in a query from a url and displays it. I have the following test.jsp file that I run on the server:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test jsp</title>
</head>
<body>
<% String firstName = (String) request.getAttribute("firstName");
out.println("Hello :" + firstName);%>
</body>
</html>
However, when I type in the following URL, I still get a "null" result (even after refreshing): see Picture.
Note: my ultimate goal is to do have an event at some point that sends a POST request from a java file and display its result in the jsp page. If I understood well from my research, I would have to do it via a Servlet with a DispacherRequest forwarding method. But I first want to understand why the simple aforementioned code isnt working).
Thank you for your help !
You mention that you're trying to display a query parameter from the URL. A query or request parameter is not the same as a request attribute.
To get a query parameter, you would have to use the getParameter() method:
String firstName = request.getParameter("firstName")

JSP Encoder Issue While sending Russian character in QueryString

I have two jsp pages. I am trying to add "Russian" language. Russian characters are shown perfectly on jsp page, but when I try to send this value to another jsp page from parameter then in second jsp page this value is changed to different characters. This problem is only in Russian Language and not in others such as Italy and French.
For example
On demo.jsp page the russian character "приветствие" is shown correctly.
but when I try to send it to another page "test.jsp" then some unknown
characters are shown like "!C<Cä5 Cô>CôCC´OD=Cä5!"
Code:
demo.jsp
String welcometext=langP.get("welcome");
<jsp:include page="<%=test.jsp%>">
<jsp:param name="wlc" value="<%=Encode.hex8Code(welcometext)%>" />
</jsp:include>
In test.jsp
String title = Encode.utfToUnicode(Decode.hex8Decode(request.getParameter("wlc")));
System.out.println(" Russial welcome test "+welcome);
Is there any special code we need to add for Russia while sending them in query parameters??
Please note* the following code are already written else it would have given problem for French and Italy language too..
<%# page contentType="text/html; charset=UTF-8" %>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
Also tried with following but didn't help out!
request.setCharacterEncoding("UTF-8")
Try to add <% request.setCharacterEncoding("UTF-8"); %> to your main jsp page:
Here is my example:
demo.jsp
<%#page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Привет</h1>
<jsp:include page="/test.jsp" flush="true">
<jsp:param name="wlc" value="Привет"/>
</jsp:include>
</body>
</html>
test.jsp
<h1>Param values is</h1>
<%
String hello = request.getParameter("wlc");
out.print(hello);
%>
I don't know the better solution but the following code solved this issue. I kept the variable in session attribute.
demo.jsp
session.setAttribute("welcometext", welcometext);
test.jsp
String welcometest=(String) session.getAttribute("welcometext");

Passing value from JSP to velocity template [duplicate]

This question already has answers here:
How do I pass get request parameters to my Velocity Template to send mails
(2 answers)
Closed 9 years ago.
Can anyone tell me how to pass value from JSP file to velocity template.
Maybe you should try to get someting simple to work. Consider a simple dynamic web project with a jsp like the following:
<%#page import="java.io.File"%>
<%#page import="org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"%>
<%#page import="org.apache.velocity.runtime.RuntimeConstants"%>
<%#page import="java.io.StringWriter"%>
<%#page import="org.apache.velocity.VelocityContext"%>
<%#page import="org.apache.velocity.Template"%>
<%#page import="org.apache.velocity.app.VelocityEngine"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String s = request.getParameter("test");
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, "/");
velocityEngine.init();
Template t = velocityEngine.getTemplate("/template.vm");
VelocityContext context = new VelocityContext();
context.put("example",
s);
StringWriter w = new StringWriter();
t.merge(context, w);
%>
<p><%out.println(w.getBuffer().toString()); %> </p>
</body>
</html>
It just loads a template from the root of your server and prints it out in the current site with a GET/POST parameter.
In the WEB-INF/lib folder of your project you put the velocity.jar, the common-collections.jar and the common-lang.jar.
Important: In your server root directory you put a simple textfile template.vm with the following template
<b>Test $example<b>
You can also have a template directory or something similar but I tell you when you wanna deploy your templates with the webapp(loading vm-files from WEB-INF directory for example)-> that's a challenge!
Load it with yourproject/example.jsp?test=getparamh and I'm sure it work
But if you try this - admitdettly - simple example, it should work and you can maybe derive your targets from it.

is this use of jstl is correct? why it does not work?

in netbeans 7 and jdk 7 and everything is working fine without any changes i made in my environment the old tags are working fine of jstl ${class.get_name()}
${page.getTitle()}
the new once i introduce does not work, and i don't know why ?
see this simple application example i created added jstl 1.2 into the libraries
and still it does not work?
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%
String var1;
var1 = "Welcome";
%>
normal : <%=var1%>
<hr />
dollar: ${var1}
</body>
</html>
First of all, the above page doesn't even use the JSTL. It uses the JSP EL.
And I assume you expect to see dollar: "Welcome" printed, but that won't happen, because the JSP EL doesn't print values of local variables. It prints the value of attributes.
Change your code to
<% pageContext.setAttribute("var1", "Welcome"); %>
or, better, to
<c:set var="var1" value="Welcome"/>
and you'll see the expected output.

Categories