ClassNotFound Exception: org.postgresql.Driver from [Module "deployment.ValidateUser.war:main" from Service Module Loader] - eclipse

I am trying to connect with postgre databse with jsp program using Eclipse IDE,
I am getting this error:
"java.lang.ClassNotFoundException: org.postgresql.Driver from [Module
"deployment.ValidateUser.war:main" from Service Module Loader]".
I had put the jar files required for my project even though i am getting classnotfound exception error.
<%#page import="com.uservalidation.FarwordNames"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<%-- Imports --%>
<%# page import="java.sql.*"%>
<%# page import="java.util.*"%>
<%# page import="javax.servlet.*" %>
<%# page import="javax.servlet.http.*" %>
<%-- HTTP header --%>
<%response.addHeader("Cache-Control","no-cache");
response.addHeader("Pragma","no-cache");
response.addHeader("Expires","0");
%>
<%-- Variables that will be written --%>
<%
String fname = request.getParameter("fname");
String mname = request.getParameter("mname");
String lname = request.getParameter("lname");
String dob = request.getParameter("dob");
String addr = request.getParameter("addr");
%>
<%-- Creating new staff account - writing --%>
<%
try{
Class.forName("org.postgresql.Driver");
String conURL= "jdbc:postgresql://192.168.1.157:5432/MIPSUITE_DEV";
Connection con = DriverManager.getConnection(conURL,"mipsuitedev01","mipsuitedev01");
Statement st = con.createStatement();
//int status = st.executeUpdate("insert into users(fname,mname,lname,dob,address) values('"+fname+"',"+mname+"','"+lname+"','"+dob+"','"+addr+"')");
/* if(status>0){
out.println("Update sucessful");
}
else{
out.println("Update unsuccessful");
} */
st.close();
con.close();
}
catch(Exception e){
out.println(e);
}
%>
</body>
</html>

It seems to be fixed the problem by removing all resteasy/jaxrs/jboss libraries from the WAR file's WEB-INF/lib/.
Put postgresql driver
jar files into WEB-INF/lib folder.
I think it's should be work for you.

Related

JSP doesn't see JavaBeans

everybody!
Can't solve this problem:
org.apache.jasper.JasperException: JBWEB004062: Unable to compile class for JSP:
JBWEB004060: An error occurred at line: 14 in the jsp file: /indexJB.jsp
packt.book.jee_eclipse.ch2.bean.LoginBean
cannot be resolved to a type
11: <%if ("POST".equalsIgnoreCase(request.getMethod())&&
12:
request.getParameter("submit")!=null)
13: { %>
14: <jsp:useBean id="loginBean"
15:
class="packt.book.jee_eclipse.ch2.bean.LoginBean">
16:
<jsp:setProperty name="loginBean" property="*"/>
17: </jsp:useBean>
I use:
WildFly 23
Eclipse Version: 2021-09 (4.21.0)
What I found in Internet:
Note
See the detailed error message under More Information.
​This issue occurs because JBoss EAP 6.2 server runtime is using unsupported JDK. Verify the following log statements for reference:
java.library.path = C:\Program Files\Java\jdk1.8.0_20\bin
java.runtime.name = Java(TM) SE Runtime Environment
java.runtime.version = 1.8.0_20-b26
Solution
​To resolve this issue, do as follows:
Point your JBoss EAP 6.2 server to a supported JDK that is JDK 1.7.x
(best is 1.7.0_25).
Restart the application server.
But how to point???
Thanks in ADVANCE!
The whole code is here:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W#C//DTD HTML 4.01 Transitional//EN"
"http://www.w3org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<% String errMsg = null; %>
<%if ("POST".equalsIgnoreCase(request.getMethod())&&
request.getParameter("submit")!=null)
{ %>
<jsp:useBean id="loginBean"
class="packt.book.jee_eclipse.ch2.bean.LoginBean">
<jsp:setProperty name="loginBean" property="*"/>
</jsp:useBean>
<%
if (loginBean.isValidUser())
{ out.println("<h2>WElcome, Admin!</h2>");
out.println("You are logged in");
}
else
errMsg = "Invalid user ID or password!";
}
%>
<body>
<h2>Login:</h2>
<%if (errMsg!=null ) {%>
<span style="color: red;"><%out.print(errMsg); %> </span>
<%} %>
<form method="POST">
User Name: <input type="text" name="userName" style="color: Green; background-color: Yellow"><br>
Password: <input type="password" name="password" style="color: Yellow; background-color: Silver"><br>
<button type="submit" name="submit">SUBMIT</button>
<button type="reset">Reset</button>
</form>
</body>
</html>
The problem was solved:
Right click on project-> choose properties -> Java Build Path -> choose third marker Libraries -> put cursor on Classpath -> on right menu choose Add Class Folder -> choose folder for classes -> Apply and Close.
I also restarted Eclipse just in case.
And my JSP finally sees Java Beans!
Hope that will help=)

How to call Java Class from JSP page? Cloud9 & OpenShift JBoss

I am new to c9. I created a Openshift JBoss application server & imported into C9 via GitHub.
I have a simple Java class in Main\Java\initial.class.
public class initial {
public static void main(String[] args) {
String prn = disp();
System.out.println(prn);
}
public static String disp()
{
String str=" JAVARANCH ";
return str;
}
}
I have a jsp page called from index.html, I am just trying to display the variable 'prn' in the jsp page.
<HEAD>
<TITLE>JBossAS7 JSP snoop page</TITLE>
<%# page import="javax.servlet.http.HttpUtils,java.util.Enumeration" %>
<%# page import="java.lang.management.*" %>
<%# page import="java.util.*" %>
<%# page import="initial" %>
</HEAD>
<BODY>
<H1>WebApp JSP Snoop page</H1>
<img src="images/jbosscorp_logo.png">
<h2>JVM Memory Monitor</h2>
<input type="text" name="text1" size="100" value="<%=prn%>">
But, I am getting below error at the JSP page:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 10 in the generated java file
The import initial cannot be resolved
An error occurred at line: 15 in the jsp file: /snoop.jsp
prn cannot be resolved to a variable
12: <img src="images/jbosscorp_logo.png">
13:
14: <h2>JVM Memory Monitor</h2>
15: <input type="text" name="text1" size="100" value="<%=prn%>">
16:
17: <table border="0" width="100%">
18:
How do I interact with a Java class from my JSP?
thanks.
EDIT due to two answers:
I think the main issue is: the class 'initial' is not being imported successfully. My call to Prn was wrong, I understand that now, but the Import itself is not working.
some suggested having the class inside a package. But Cloud9 IDE does not have an option to create a package.
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 10 in the generated java file
The import initial cannot be resolved
If you try this :
Java class:
public class Initial {
public static String getDisp() {
return "JAVARANCH";
}
}
JSP file :
<HEAD>
<TITLE>JBossAS7 JSP snoop page</TITLE>
<%# page import="javax.servlet.http.HttpUtils,java.util.Enumeration" %>
<%# page import="java.lang.management.*" %>
<%# page import="java.util.*" %>
<%# page import="Initial" %>
</HEAD>
<BODY>
<H1>WebApp JSP Snoop page</H1>
<img src="images/jbosscorp_logo.png">
<h2>JVM Memory Monitor</h2>
<input type="text" name="text1" size="100" value="<%=Initial.getDisp()%>">
</BODY>
You can access your class from your jsp by adding the following.
<input type="text" name="text1" size="100" value="<%= initial.disp() %>">
or by adding a jsp scriptlet prior to your input
<%
String prn = initial.disp();
%>
<input type="text" name="text1" size="100" value="<%= prn %>">
Notice I am using your method disp() not main(). Main is static void with no return.

how can i overcome this compilation error?

Actually i am new to eclipse and jdbc connections i tried to connect to hive database on my centos and am trying to perform queries on a table in hive database through the following code.And am facing compilation problem for this code.plz help me..
<%#page import="org.apache.hadoop.hive.jdbc.HiveDriver"%>
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# page import = "java.sql.*" %>
<%# page import = "java.math.*" %>
<%# page import = "java.util.Map.Entry"%>
<%
try {
Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver").newInstance();
}
catch(ClassNotFoundException ex){
System.out.println("error:unable to load driver class");
System.exit(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=UTF-8">
<title>server side</title>
</head>
<body>
<%
Connection connection = DriverManager.getConnection("jdbc:hive://localhost:10000/nikhildb");
Statement statement = connection.createStatement();
String country_name = request.getParameter("country_name");
ResultSet rs = statement.executeQuery("Select * from population where country_name ='"+country_name+"'");
if(!rs.next()){
System.out.println("Sorry, no country exists");
}
else{
%>
i had some table details in html after else statement.
this is the photo copy of compilation error occured
plzz help me..!
Your HiveDriver is a packages - "org.apache.hadoop.hive.jdbc.HiveDriver resolves to a package", so the correct way to import it is:
<%#page import="org.apache.hadoop.hive.jdbc.HiveDriver.*"%>
I can't say, where and how many time you have to fix it, though.

'esapi' is undefined in Websphere 7

Have been following the esapi4java-core-2.0-install-guide.pdf for Eclipse and applying it in a Websphere 7.5 environment.
Step 1 - I added the esapi.jar in the libraries (Project > Properties > Java Build Path > Libraries tab > Add External JARs).
Step 2- I located the esapi & validation .properties files in a folder on my machine.
Step 3 - I selected to add esapi for all run configurations (Windows > Preferences > Java > Installed JREs). I highlighted WebSphere Application Server v7.0 JRE, selected "Edit" and added esapi.jar to that library list. On that same form, I added the -Dorg.owasp.esapi.resources="/path/to/.esapi" argument to the Default VM Arguments prompt, the path to where my esapi & validation .properties files are located.
Here's the code on the JSP page that fails:
<%# page language="java" %>
<%# taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%# taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%# taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%# page import="java.net.*" %>
<script type="text/javascript" language="javascript">
function validateForm()
{
var userURL = "http://www.google.com";
var isValidURL = ESAPI.validator().isValidInput("URLContext", userURL, "URL", 255, false);
if (isValidURL) {
alert("true");
} else {
alert("false");
}
}
</script>
Upon executing this bit of code, I get the error:
'ESAPI' is undefined (which happens on the var isValidURL statement).
What am I missing?
I don't see where you imported esapi in the jsp.
Try this:
<%# page language="java" %>
<%# taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%# taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%# taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%# page import="java.net.*, org.owasp.esapi.ESAPI, org.owasp.esapi.Validator" %>
<script type="text/javascript" language="javascript">
function validateForm()
{
var userURL = "http://www.google.com";
var isValidURL = <% ESAPI.validator().isValidInput("URLContext", userURL, "URL", 255, false); %>
if (isValidURL) {
alert("true");
} else {
alert("false");
}
}
</script>
Also note, I'd highly suggest doing the validation in the receiving controller and not in the jsp... the overhead for JSP compilation is murder without adding the overhead of parsing input. The other warning I'll give you is that if you note esapi documentation, calling isValidInput() ignores canonicalization which is absolutely critical. You'll want to use getValidInput() instead. If you feel you need to use isValidInput() then you'll want to ensure you make a manual call to canonicalize() which is a method in the Encoder class. Also, please note that this line:
var isValidURL = ESAPI.validator().isValidInput("URLContext", userURL, "URL", 255, false);
is making a critical mistake in that ESAPI is not a JavaScript library. This call should look like this:
var isValidURL = <% ESAPI.validator().isValidInput("URLContext", userURL, "URL", 255, false); %>
And in general, as stated above, you want to do this validation on the controller that will be using this jsp as input, not on the jsp itself. (Scriptlets are slow and should be avoided.)

Return UTF-8 encoding from an Action in Struts 2

I'm trying to output a list of strings to a jsp page using struts 2. The template file is something like this:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<s:iterator value="vars">
<p><s:property /></p>
</s:iterator>
</body>
</html>
The corresponding java action file is:
public class TestAction extends ActionSupport {
private static final long serialVersionUID = 1L;
List<String> vars = null;
public List<String> getVars() { return vars; }
public void setVars(List<String> vars) { this.vars = vars; }
#Override
public String doExecute() throws Exception {
vars = new ArrayList<String>();
vars.add("Users");
vars.add("Organizações");
vars.add("Eventos");
vars.add("Locais");
return SUCCESS;
}
}
The problem is that, the result is:
Users
Organizações
Eventos
Locais
(source code)
<p>Users</p>
<p>Organizações</p>
<p>Eventos</p>
<p>Locais</p>
which I believe it means that I'm receiving the strings encoded with something that's not UTF-8. Any ideas on how to solve this?
This is very late reply, but I want to update this to help, somebody who stuck with this bug. Add following lines to your jsp to resolve the issue.
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<s:property escapeHtml="false"/>
Depending on your version of struts 2
Older: (this is deprecated in newer versions)
<s:property escape="false"/>
Newer: (not sure which version this was introduced)
<s:property escapeHtml="false"/>