Calling Java class in JSP - java

I've done my fair share of research and from what I've seen, I have everything correct. I'm trying to call a Java class in my JSP file. Every time I run it, I get the following error:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 25 in the jsp file: /3680-HW4-Taylor-Jacob/HTML/main.jsp
DateAssistant.formatDate cannot be resolved to a type
22: out.println("Welcome, " + request.getParameter("usrname") + "!");
23:
24: if (timezone.equalsIgnoreCase("United States")){
25: out.write("You logged in on: " + new DateAssistant.formatDate(gc, timezone) + "");
26:
27: }
28: out.write("Browse Movies Catalog");
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:468)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
I have the Class file in the correct place (WEB-INF/classes/wu/andy/DateAssistant.class), and I've imported correctly from what I've seen. Here is my JSP source code:
<%# page import="java.util.GregorianCalendar, wu.andy.*" %>
<%
String interest = request.getParameter("interest");
String password = request.getParameter("password");
GregorianCalendar gc = new GregorianCalendar();
int tz = 0;
String timezone = request.getParameter("country");
if(password.equals("JTaylor")){
if(interest.equals("music")){
response.sendRedirect("music.html");
}
else if(interest.equals("games")){
response.sendRedirect("games.html");
}
else if(interest.equals("movies")){
out.write("<html>");
out.write("<head/>");
out.write("<body>");
out.write("<h1 style='color:green'>Movies Galore :: User Main Page</h1>");
out.println("Welcome, " + request.getParameter("usrname") + "!");
if (timezone.equalsIgnoreCase("United States")){
out.write("<p>You logged in on: " + new DateAssistant.formatDate(gc, timezone) + "</p><br/><br/>");
}
out.write("<a href='movies.html'>Browse Movies Catalog</a>");
}
} else{
response.sendRedirect("login.html");
}
%>
I know my code is not "good practice" but this is how my professor wants it.
Any help would be appreciated!

Looks like you forgot to import DateAssistant class in jsp header.
<%# page import="java.util.GregorianCalendar, wu.andy.*, XXXXX.DateAssistant" %>

Your JSP line:
out.write("<p>You logged in on: " + new DateAssistant.formatDate(gc, timezone) + "</p><br/><br/>");
Uses the new keyword next to DateAssistant.formatDate(). Unless this is a public inner class I think this is a static method in which case you should drop the new

Related

JSP error at line 31

When i am running the jsp page drops me this error , any suggestion in order to fix it , (i know jsp with java its not good)...
type Exception report
message An exception occurred processing JSP page
/assets/jsp/create-dest-code.jsp at line 31
description The server encountered an internal error that prevented it
from fulfilling this request.
exception org.apache.jasper.JasperException: An exception occurred
processing JSP page /assets/jsp/create-dest-code.jsp at line 31
28: if(id > -1) 29: { 30: int insert_ch; 31: for(int
i=0;i
insert_ch=myStatement.executeUpdate("INSERT INTO Dest_has_Categories
(Dest_idDest,Categories_idCategories) VALUES
('"+id+"','"+cat[i]+"')"); 34: }
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause java.lang.NullPointerException
org.apache.jsp.assets.jsp.create_002ddest_002dcode_jsp._jspService(create_002ddest_002dcode_jsp.java:136)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache
Tomcat/8.0.33 logs.
my code :
<%
String id8=request.getParameter("id8"); //Country field
String id9=request.getParameter("id9"); //City field
String id10=request.getParameter("id10"); //URL field
int id = -1;
Class.forName("com.mysql.jdbc.Driver");
String myDatabase = "jdbc:mysql://localhost:3306/project_app?user=root&password=1234";
Connection myConnection = DriverManager.getConnection(myDatabase);
Statement myStatement = myConnection.createStatement();
String sqlInsert = "INSERT INTO dest(Country,City,URL) VALUES ('"+id8+"', '"+id9+"','"+id10+"')";
myStatement.executeUpdate(sqlInsert);
ResultSet rs = myStatement.executeQuery("SELECT idDest FROM dest WHERE Country='"+id8+"' AND City='"+id9+"' AND URL='"+id10+"'" );
while (rs.next()) {
id=rs.getInt(1);
}
String cat[]=request.getParameterValues("dest1");
if(id > -1)
{
int insert_ch;
for(int i=0;i<cat.length;i++)
{
insert_ch=myStatement.executeUpdate("INSERT INTO Dest_has_Categories (Dest_idDest,Categories_idCategories) VALUES ('"+id+"','"+cat[i]+"')");
}
}
myStatement.close();
myConnection.close(); %>
<h1 style="color:blue;">Successful Registration </h1>
I assume that once you know what is the line #31, you will be able to fix your NPE thanks to many other questions of this type so let's focus on how to find the line #31 in your case.
According to your stacktrace you are using Tomcat 8, so the source code of your jsp file /assets/jsp/create-dest-code.jsp is in $TOMCAT_HOME/work/Catalina/localhost/assets/org/apache/jsp/jsp/create-dest-code_jsp.java, from here check the line #31 and you will know how to fix it.
Actually you have already the answer here:
28: if(id > -1) 29: { 30: int insert_ch; 31: for(int i=0;i
insert_ch=myStatement.executeUpdate("INSERT INTO Dest_has_Categories
(Dest_idDest,Categories_idCategories) VALUES
('"+id+"','"+cat[i]+"')"); 34: }
It seems that your variable cat is null which probably means that you have no value for the parameter dest1.
Are you sure you connection string is valid? In the example here There is more details in the connection string that is missing in your code (like database name) :
Connection con = null;
try {
con = DriverManager.getConnection(
"jdbc:sqlserver://localhost\\SQLEXPRESS;"
+ "user=sa;password=HerongY#ng;"
+ "database=AdventureWorks2014");
source

JSP Exception error while converting from String to Int

There is one class as X.Java.In this below two lines I am doing
request.getSession.setAttribute("count",String.valueOf(rsp.getCount()));
In the above I am getting count from database in rsp.getCount(). Here getcount() is integer
type and I am changing to String as String.valueOf(rsp.getCount() and setting into one attribute as count.
There is one jsp as X.jsp
Now I am using getAttribute and storing the value in failCount variable of string type.
String failCount = request.getSession.getAttribute("count");
Now I want to convert this value into integer type for that I wrote this line
int countInt = Integer.parseInt(failCount);
I am getting error like the one below
127039 13-02-26 17:13:35 ERROR http-10.18.2.105-12205-Processor4 - <JSPFilter ServletException> - test.abc.common.filter.JSPFilter.doFilter(JSPFilter.java:111)
org.apache.jasper.JasperException: Exception in JSP: abc/x.jsp:66
63:
64: }
65: String failCount = (String)request.getSession().getAttribute("failCount");
66: int failCountint = Integer.parseInt(failCount);
67: //System.out.println(failCountint);
68:
69:
Stacktrace:
at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:489)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:679)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:461)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:399)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:142)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:247)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1105)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:841)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:755)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:396)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:350)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
Please help me fix this.
Make sure you are setting session in HttpRequest
request.getSession.setAttribute("count",String.valueOf(rsp.getCount()));
Is this java file is servlet then it will work well.
One more thing make sure rsp.getCount() is not return "null".
In JSP file
String failCount = (String)request.getSession().getAttribute("failCount");
int failCountint = Integer.parseInt(failCount);
why are you using request.getSession() instead of session, and you are trying to get failcount instead of count
Try this
String failCount = session.getAttribute("count").toString();
int countInt = Integer.parseInt(failCount);
I think it may help full...
Setting value is session:
request.getSession.setAttribute("count",String.valueOf(rsp.getCount()));
Reading value from session:
String failCount = (String)request.getSession().getAttribute("failCount");
While setting the value in session, you are calling it count and while reading the value from JSP, you are calling it failCount.
I think failCount is null that why
int failCountint = Integer.parseInt(failCount);
So please use
System.out.println(failCount);
And also use try catch block around error line.

Error when trying to search similar images using JSP

I am building a web application for feature extraction and comparing the feature of images, using JSP, Java, and PostgreSQL.
My feature extraction is extracting some features from images, so that then I can compare them (some features are about color, some on the histogram of the image and so. I have special methods that extract them using specific libraries (jars that I add into lib) as you can see in the following example!
Those features are then successfully being added into my PostgreSQL database.
public void GetImageProperties(String targetPath, ImageProperties imagesProperties) throws Exception
{
// load image
FileInputStream imageStream;
imageStream = new FileInputStream(targetPath);
BufferedImage bimg = ImageIO.read(imageStream);
// get properties
imagesProperties.cl = new ColorLayoutImpl(bimg);
imagesProperties.eh = new EdgeHistogramImplementation(bimg);
imagesProperties.sc = new ScalableColorImpl(bimg);
}
Then I add them into a postgresql database(only the features, not the images)
String query = "CREATE TABLE Images (" +
" imageid SERIAL PRIMARY KEY," +
" fname VARCHAR(128)," +
" colorlayout VARCHAR(512), " +
" edgehist VARCHAR(512), " +
" scalablecolor VARCHAR(512) " +
")";
I get this error every time I had loaded an image and want to compare it with all the other images in a directory.
HTTP Status 500 -
type Exception report
message:
description The server encountered an internal error () that prevented it from fulfilling this request.
exception:
org.apache.jasper.JasperException: An exception occurred processing JSP page /query.jsp at line 124
121: if (bSearch) {
122: // get form params
123: query.setTopK(new Integer(request.getParameter("frm_topk")));
124: query.setColorHistogramWeight(new Integer(request.getParameter("frm_weight_c1")));
125: query.setColorDistributionWeight(new Integer(request.getParameter("frm_weight_c2")));
126: query.setTextureWeight(new Integer(request.getParameter("frm_weight_c3")));
127: }
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
root cause:
java.lang.NumberFormatException: For input string: "1.0"
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
java.lang.Integer.parseInt(Integer.java:492) java.lang.Integer.<init>
(Integer.java:677) org.apache.jsp.query_jsp._jspService(query_jsp.java:256)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs.
Your problem is here
query.setTopK(new Integer(request.getParameter("frm_topk")));
should be
String parFrm_topk = request.getParameter("frm_topk");
Integer frm_topk = null;
if (parFrm_topk != null && parFrm_topk.length() > 0 && !parFrm_topk.equals("null"))
try {
frm_topk = Integer.valueOf(parFrm_topk);
} catch (NumberFormatException nfe){
throw nfe;
}
if (frm_topk ! = null) {
query.setTopK(frm_topk);
this code is trying to check the parameter that could be empty, why it is empty depends on how flow the app and how parameters are passed through url.

final variable not working in jsp

what was i doing wrong ??
I am running Apache tomcat 7 as a service in win 7
my jsp code [...\webapps\test\index.jsp ]
<jsp:useBean id="Counter" scope="session" class="aaa.Counter" />
<html>
<title>sfcsv</title>
<%
try{
int x = Counter.read_count(),z = Counter.get_id();
if(x%2==0)
out.println(x + " = even");
else
out.println(x + " = odd");
out.println(z);
}catch(Exception e){
out.println(e);
}
%>
</html>
the java code [..\webapps\test\WEB-INF\classes\aaa\Counter.java ]
package aaa;
public class Counter {
private int count;
private static int instance_counter;
private final int id;
public Counter(){
instance_counter ++;
id = instance_counter;
count = 0;
}
public int read_count(){
return count++;
}
public int get_id(){
return id;
}
}
expected output :
24 = even 1
output that I was getting :
24 = even
or
HTTP Status 500 - Unable to compile class for JSP: An error occurred at line: 8 in the jsp file: /index.jsp The method get_id() is undefined for the type Counter 5: 6: <% 7: try{ 8: int x = Counter.read_count(),z = Counter.get_id(); 9: if(x%2==0) 10: out.println(x + " = even"); 11: else Stacktrace:
type Exception report
message Unable to compile class for JSP: An error occurred at line: 8 in the jsp file: /index.jsp The method get_id() is undefined for the type Counter 5: 6: <% 7: try{ 8: int x = Counter.read_count(),z = Counter.get_id(); 9: if(x%2==0) 10: out.println(x + " = even"); 11: else Stacktrace:
description The server encountered an internal error (Unable to compile class for JSP: An error occurred at line: 8 in the jsp file: /index.jsp The method get_id() is undefined for the type Counter 5: 6: <% 7: try{ 8: int x = Counter.read_count(),z = Counter.get_id(); 9: if(x%2==0) 10: out.println(x + " = even"); 11: else Stacktrace:) that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 8 in the jsp file: /index.jsp
The method get_id() is undefined for the type Counter
5:
6: <%
7: try{
8: int x = Counter.read_count(),z = Counter.get_id();
9: if(x%2==0)
10: out.println(x + " = even");
11: else
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:469)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.29 logs.
Apache Tomcat/7.0.29
EDIT: On restarting the computer the problem vanished
You already solved your problem but here are the appropriate options to get all your classes and JSP files to be reloaded or recompiled which might help in understanding what was going on in your case:
Refreshing Java classes - Be sure to recompile your classes after applying changes to them. Also see the reloadable option for you web application context (default is false).
Set to true if you want Catalina to monitor classes in /WEB-INF/classes/ and /WEB-INF/lib for changes, and automatically reload the web application if a change is detected.
Refreshing JSP files - Please see the Tomcat Jasper docs for details on the development variable (defaults to true so I guess this is its value in your case) and the modificationTestInterval variable (defaults to 4 seconds) with which you may set the interval in which JSP files are checked for changes and compiled if necessary. Both are usually set in $CATALINA_BASE/conf/web.xml.
Is Jasper used in development mode? If true, the frequency at which JSPs are checked for modification may be specified via the modificationTestInterval parameter.true or false, default true.
I think its not problem with your code.I think your IDE is not building your project correctly
ie .class file is not generated properly( or not generated). so try to compile this file manually and place generated .class file to appropriate folder of your project and then build and restart your server.Check on build its building new .JAR file or not

Object Request with JSP

I have a problem with a JSP file. I want to retrieve the header but I have an error:
Une erreur s'est produite à la ligne: 45 dans le fichier jsp: /logTest.jsp
Enumeration cannot be resolved to a type
42: String headerString = "";
43: String name = "";
44:
45: for (Enumeration en = request.getHeaderNames(); en.hasMoreElements(); ) {
46: name = (String)en.nextElement();
47: headerString = headerString + name + "=<B>" + request.getHeader(name) + "</B><BR>";
48: }
My object (request) is not null so I don't understand why I have this error.
Can you help me ?
Thanks.
You have to explicitly import java.util.Enumeration. The stack trace gave you the answer: "Enumeration cannot be resolved to a type".

Categories

Resources