JSP class attribute invalid - java

I am attempting to get to grips with a decent level of JSP quickly to build an interface. I found what seems a good tutorial at this URL.
Reference link
While following the tutorial to the word, on page 35 it covers jsp:getProperty Action. I successfully created the TestBean.java file and it compiled to produce TestBean.class. But what I found was that my Tomcat's webapps directory did not contain a folder named "WEB-INF" and so no directory "webapps\WEB-INF\classes\action" so I added the "WEB-INF\classes\action" myself. Then when I run the main.jsp file the browser states the following error
"The value for the useBean class attribute action.TestBean is invalid"
What I am doing wrong?
Here is the Java "TestBean.class" code;
/* File: TestBean.java */
package action;
public class TestBean {
private String message = "No message specified";
public String getMessage() {
return(message);
}
public void setMessage(String message) {
this.message = message;
}
}
And here is the JSP "main.jsp" code
<html>
<head>
<title>Using JavaBeans in JSP</title>
</head>
<body>
<center>
<h2>Using JavaBeans in JSP</h2>
<jsp:useBean id="test" class="action.TestBean" />
<jsp:setProperty name="test" property="message" value="Hello JSP..." />
<p>Got message....</p>
<jsp:getProperty name="test" property="message" />
</center>
</body>
</html>

I am assuming you are working on eclipse.
If Web-INF is not created that means you are not working in a dynamic web project.
Go to NEW in eclipse. Go to Dynamic web project. It will create all you folders needed for a JSP/WEB project itself including Web-INF. This will stream line the process.
Other option is place your class files in required folder which is quite tedious job. Hope it helps.

Related

Running a servlet in intelliJ from .java class, not .jsp

I'm trying to create and run a servlet in IntelliJ. The problem I'm having is I'm following an Eclipse tutorial and they seem to work very differently. In Eclipse, a servlet.java class is created and run on Tomcat. In IntelliJ a .java class and .jsp file is created. The browser points to .jsp, not .java. The java class doesn't seem to be doing anything at all.
Why are they so different, and how can I point to the .java class instead of the .jsp?
I've added the .java and .jsp code below, which are both the standard stubs created by IntelliJ when creating a new servlet project.
#WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
private String message;
public void init() {
message = "The Tomcat server does not point to this code in IntelliJ";
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
// Hello
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>" + message + "</h1>");
out.println("</body></html>");
}
public void destroy() {
}
}
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Tomcat points to this .jsp file, not the .java code" %>
</h1>
<br/>
Hello Servlet
</body>
</html>
The servlet class has an annotation indicating what URI to use, so whatever URL your application is accessed using, followed by /hello-servlet.
Kind of weird that they generate these files that'll be useless 99% of the time.

Java JSP Using Include with Variable Parameters

I'm trying to pass a parameter to the jsp file that I am including in my main jsp. From what I've seen online the way to do this using c:set
approot/index.jsp
<c:set var="Arg01" value="Argument01"/>
<jsp:include page="include/other.jsp">
<jsp:param name="myArg01" value="${Arg01}"/>
<jsp:param name="myArg02" value="Argument02"/>
</jsp:include>
Although when I try to use the variables in the included jsp page only the one argument seems to be coming through (the second one which is not using c:set)
approot/include/other.jsp
<!-- this doesn't work -->
<p>${param.myArg01}</p>
<!-- this does -->
<p>${param.myArg02}</p>
Nothing crashes but I can see that myArg01 is blank
This way of getting around it is probably awful but it's the only way I could find to get around the issue.
I used a different kind of include in my main JSP
public static String myArg01 = "Argument01";
public static String myArg02 = "Argument02";
<%# include file="include/other.jsp" %>
Then I could reference the variables directly inside the included JSP file
<p><%= myArg01 %></p>
<p><%= myArg02 %></p>

How to access environment variables from JSP page

How can I access environment variables from a JSP page? Does one of the implicit objects give access to them? I couldn't find an example addressing this specific issue. Ideally I'm looking for something like:
<c:set var="where" value="${myEnvironment.machineName}">
You can read the properties file at the server start-up using ServletContextListener and store it as application scoped attribute to access it from anywhere in the application.
Steps to follow:
.properties:
machineName=xyz
web.xml:
<listener>
<listener-class>com.x.y.z.AppServletContextListener</listener-class>
</listener>
AppServletContextListener.java:
public class AppServletContextListener implements ServletContextListener {
private static Properties properties = new Properties();
static {
// load properties file
try {
// absolute path on server outside the war
// where properties files are stored
String absolutePath = ..;
File file = new File(absolutePath);
properties.load(new FileInputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
#Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
servletContextEvent.getServletContext().
setAttribute("myEnvironment", properties);
}
}
JSP:
Then you can just treat it as Map in EL.
${myEnvironment['machineName']}
or
${myEnvironment.machineName}
Read more about JSTL Core c:set Tag
The <c:set> tag is JSTL-friendly version of the setProperty action. The tag is helpful because it evaluates an expression and uses the results to set a value of a JavaBean or a java.util.Map object.
The <c:set> tag has following attributes:
If target is specified, property must also be specified.
Read more about it HERE
If you are looking for sample code then find it here. Please find it at below posts. It might help you.
Acces value between two jsp with jstl
JSP - Standard Tag Library (JSTL) Tutorial
More samples on other scopes.
<%-- Set scoped variables --%>
<c:set var="para" value="${41+1}" scope="page" />
<c:set var="para" value="${41+1}" scope="request" />
<c:set var="para" value="${41+1}" scope="session" />
<c:set var="para" value="${41+1}" scope="application" />
<%-- Print the values --%>
<c:out value="${pageScope.para}" />
<c:out value="${requestScope.para}" />
<c:out value="${sessionScope.para}" />
<c:out value="${applicationScope.para}" />
In your case you have set an attribute where in default page scope.

get parameter for JApplet not working

I am experiencing a little problem reading parameters from an html page to an applet.
my code (necesssary for question):
on html page:
<PARAM NAME = "name" VALUE = "Nicholus">
in applet (init):
String strName = getParameter("name");
The applet just decides to look at me instead of getting the name value..
a few google search shows im not the only one, except I haddn't yet found the solution, so I decided to post it here in case it was already resolved.
Compare your work with the following working code or post your code if problem still not solved.
Here is the ParamDemo.java code
import java.awt.*;
import java.applet.*;
public class ParamDemo extends Applet{
String strName;
public void start()
{
strName=getParameter("name");
if(strName==null)
strName="Not Found";
}
public void paint(Graphics g)
{
g.drawString("Name :"+strName,10,20);
}
}
Then the Applet.html
<html>
<body>
<applet code="ParamDemo" width="300" height="300">
<param name='name' value="Nicholas">
</applet>
</body>
</html>
Snapshot
[Ps:You need to compile your .java before using in HTML page and make sure that there is no problem related to leter-case of parameter-names as java is case-sensitive.Also the .class and .html files must be in same root folder).
I got it solved, my negligence... my html had many applet codes, the params were not printed within the applet declaration of the one that needs the params. thanks for contributing.

Spring Web Services Class Not Found Exception

I'm going through this Spring tutorial online form springsource.org.
http://static.springsource.org/docs/Spring-MVC-step-by-step/part2.html
In Chapter 2, at the end, it has you add a bean to prefix and suffix /WEB-INF/jsp/ and .jsp to responses.
The code so far should basically load index.jsp when you go to localhost:8080/springapp/ which will redirect to localhost:8080/springapp/hello.htm which creates an instance of the HelloController which should in theory send you over to /WEB-INF/jsp/hello.jsp. When I added the prefix/suffix bean and changed all my references to just "hello" instead of the fully pathed jsp file, I started getting the following error:
message Handler processing failed; nested exception is
java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/fmt/LocalizationContext
I've tried going back through the samples several times and checking for typo's and I still can't find the problem. Any tips or pointers?
index.jsp (in the root of the webapp:
<%# include file="/WEB-INF/jsp/include.jsp" %>
<%-- Redirected because we can't set the welcome page to a virtual URL. --%>
<c:redirect url="/hello.htm" />
HelloController.java (minus the imports and package:
public class HelloController implements Controller {
protected final Log logger = LogFactory.getLog(getClass());
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String now = (new Date()).toString();
logger.info("Returning hello view with " + now);
return new ModelAndView("hello", "now", now);
}
}
My hello.jsp file:
<%# include file="/WEB-INF/jsp/include.jsp" %>
<!DOCTYPE html>
<html>
<head>
<title>Hello :: Spring Application</title>
</head>
<body>
<h1>Hello - Spring Application</h1>
<p>Greetings, it is now <c:out value="${now}" /></p>
</body>
</html>
It seems like you are missing the JSTL jar here. Try downloading it and place it in your classpath to see if it works: Where can I download JSTL jar
It seems certain required jar(s) are missing from classpath.
Make sure you have servlet-api-2.x.jar jsp-api-2.x.jar and jstl-1.x.jar on classpath
Please make sure the jstl.jar file is located in your WEB-INF/lib folder.
As a matter of fact, here is what is stated in the tutorial that you linked. I guess you missed this step:
We will be using the JSP Standard Tag Library (JSTL), so let's start
by copying the JSTL files we need to our 'WEB-INF/lib' directory. Copy
jstl.jar from the 'spring-framework-2.5/lib/j2ee' directory and
standard.jar from the 'spring-framework-2.5/lib/jakarta-taglibs'
directory to the 'springapp/war/WEB-INF/lib' directory.

Categories