JDBC driver not found for PostgreSQL database while building with Maven - java

I am following a course "Learning Path: Enterprise Web Programming with Java" by O'Reilly. I have re-written code for a simple webapp using PostgreSQL. I build it with Maven and run it on a Tomcat server. The database is up and filled. I receive the following error:
javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver found for ${ourDS}"
I have found that it is usually a result of no postgres dependency. I have also tried adding other dependencies like tomcat plugin, etc.
I understand that I miss a small piece of dependency or some file somewhere, but I can not find out what is it.
The main .jsp file has a following code:
<%# page errorPage = "error.jsp" %>
<%# taglib uri = "http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib uri = "http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<!DOCTYPE html>
<html>
<head>
<title>List of products in our database</title>
<link rel = "stylesheet" href = "style.css" type = "text/css"></link>
</head>
<body>
<sql:setDataSource
var = "ourDS"
driver = "org.postgresql.Driver"
url = "jdbc:postgresql://localhost:5432/skistuff"
user = "******"
password = "*******"
/>
<sql:query var = "productList" dataSource = "${ourDS}">
SELECT * FROM skisEtc ORDER BY id;
</sql:query>
<!-- Some kind of display -->
</body>
In contrary to the lecturer, I use Maven instead of Ant, so I have to figure it out other way. From what was mentioned in the course, I prepared the following pom file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.training.skis</groupId>
<artifactId>skispagebasic</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>skispagebasic Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.1.4.jre7</version>
</dependency>
</dependencies>
<build>
<finalName>skispagebasic</finalName>
</build>
</project>

Related

HTTP Status 404 - not found - Java Spring MVC

I'm trying to run my first Spring MVC Program on eclipse using XML configuration, I have no error markings on my directory but I get this response when I run the program. I also tried with Spring java configuration and got similar response.
bug
I use eclipse Version: 2021-06 (4.20.0) and apache-tomcat-10.0.13 installed on my device, sending a request at http://localhost:8080/ returns apache-tomcat successfully installed. So far I have tried all suggestions I found online including updating maven project, maven build-clean install, cleared file cache, deleted temp folder in my root director changed workspace directory, uninstalled and reinstall apache-tomcat multiple times, cleared missing files on source directory in java build path but none of this approaches have worked. Below is a screenshot of my program (XML configuration)
Directory
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.idevelope</groupId>
<artifactId>spring-mvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Spring MVC</name>
<description>Spring MVC example</description>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>16</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
</plugin>
</plugins>
</build>
</project>
web.xml
dispatcher-servlet.xml
controller
user.java
(View Pages)
home.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Spring MVC tutorial - Home JSP</title>
</head>
<body>
<h1>Hello World!</h1>
<h4>Message- </h4><span>${message}</span>
<form:form action="showUser" modelAttribute="user" method="post">
<tr>
<td>
<form:label path="firstName">First Name</form:label>
</td>
<td>
<form:input path="firstName" id="firstname" />
</td>
</tr>
<tr>
<td>
<form:label path="lastName">Last Name</form:label>
</td>
<td>
<form:input path="lastName" id="lastname" />
</td>
</tr>
<input type="submit" value="Submit">
</form:form>
</body>
</html>
user.jsp
Please I need help to fix this bug.
In my case and as i searched internet - tomcat 10 does not work. I downloaded tomcat 9 and 404 disappeared. Spring MVC 5 does not work on Tomcat 10. This is because Tomcat 10 is based on Jakarta EE 9 where package names for APIs have changed from javax.* to jakarta.*.
maybe lack
*<mvc:annotation-driven/>*Annotation-driven injection (e.g. Controller, #requestMapping, etc.) results in a page always indicating that the relevant resource information cannot be accessed
You write, you try to run your first spring program with Spring MVC. I did not dive into your details, but I would suggest two different approaches to learn and play around with Spring MVC:
Spring initializer web site
When I try out new spring parts, I always start at
https://start.spring.io/
and just add my wanted dependencies (search for mvc when pressing add button).
After defined your setting there, you can just download a working maven or a gradle example directly from the web page as a zip file. After extraction the files can be directly imported by IDE (e.g. eclipse, intelliJ, etc.)
Official Spring MVC tutorial
When you are searching a good and out-of-the-box working MVC example you should look at https://spring.io/guides/gs/serving-web-content/
This works well and you can start directly without dependency problems etc.
Try adding tomcat jasper dependency to pom.xml
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>10.1.0-M5</version>
</dependency>
check tomcat version before adding the dependency

Jar file not working in target folder but working in project folder

When I run the project in STS, it's working fine. All the pages in the webapp folder are loading in the browser. But when I build the project and run the JAR file (folder: D:\SpringBoot Projects\DemoExample\target) and go to the page URL, I'm getting Whitelabel error.
In another project I used spring security- In that project the login page is loading fine, but all the JSP pages in the webapp folder are not loading just like the above example.
While trying to solve this problem, I moved the JAR file from target folder to project folder (D:\SpringBoot Projects\DemoExample) and again ran the JAR file, now the JSP pages are loading fine. The JSP pages are only loading in that folder, if I moved the file to another folder, the pages are not loading.
I want to make sure that I can run the JAR file from any folder.
POM File:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.project</groupId>
<artifactId>DemoExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DemoExample</name>
<description>Project</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.37</version>
</dependency>
</dependencies>
<build>
<outputDirectory>${basedir}/${target.dir}/classes</outputDirectory>
<testOutputDirectory>${basedir}/${target.dir}/test-classes</testOutputDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Main Class:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoExampleApplication {
public static void main(String[] args) {
SpringApplication.run(DemoExampleApplication.class, args);
}
}
Controller:
package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
#org.springframework.stereotype.Controller
public class Controller {
#RequestMapping("/")
public String home() {
return "home.jsp";
}
}
JSP File:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hello
</body>
</html>
Project Structure
Running Jar file in target folder
Result after running Jar file in target folder
Running Jar in project folder
Result after running Jar file in project folder

why isn't Spring MVC finding the path to my views?

I'm new to Spring and I'm trying to make a simple web app, however I cannot get started with the basics and cant make even a HelloWorld application.
Here is what I am Using:
Spring 5
Spring boot 2.1
Eclipse Photon, I used the add-on Spring Tools Version 3.9.6 to create the proyect
Here is what I did:
Created via File -> New -> Spring Starter Proyect, selected type package type as WAR, also in the dependencies section I selected Web
Added jstl and jasper dependencies to the pom.xml, here is the whole file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.logback.app</groupId>
<artifactId>spring-boot-logback</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-boot-logback</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
**<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>**
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
My view template is as follows:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1" />
<title>Logback</title>
</head>
<body>
<h1>Hello world</h1>
<h2><c:out value="${titulo}"/></h2>
</body>
</html>
This is my controller:
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
#Controller
public class IndexController {
#GetMapping("/index")
public String index(Model model) {
model.addAttribute("titulo", "Pruebas Logback");
return "index";
}
}
And I added these to the application.properties file
spring.mvc.view.prefix: /WEB-INF-/views/
spring.mvc.view.suffix: jsp
With all these configuration it should work, but when I enter the address localhost:8080/index it throws what it looks like a 404 error
And the strangest thing is that neither in the web browser console or in the Eclipse console are errors that point out to the right direction.
What do you think it can be?
Thanks ind advance
Thanks to Piotr Podraza I realized my mistake
Changed
spring.mvc.view.prefix: /WEB-INF-/views/
spring.mvc.view.suffix: jsp
to
spring.mvc.view.prefix: /WEB-INF/views/
spring.mvc.view.suffix: .jsp
The path to my views was incorrect, and I was missing the period of the jsp suffix
Thank you!

JSP Parameters not accessible using ${param.xxx} using HTML form

I'm following a tutorial on Udemy that explains JSPs and Servlets:
https://www.udemy.com/jsp-tutorial
The tutorial uses Eclipse + Tomcat server.
Since I'm an IntelliJ and Maven user I wanted to set up my environment using these two. So I created a Maven project from the following archetype: "org.apache.maven.archetypes:maven-archetype-webapp" and configured my POM as folllows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SomeGroupId</groupId>
<artifactId>HelloWorldJavaServerPages</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>HelloWorldJavaServerPages Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>HelloWorldJavaServerPages</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<server>Tomcat85Localhost</server>
<username>admin</username>
<password>admin</password>
</configuration>
</plugin>
</plugins>
</build>
</project>
Set up my other properties as described here:
Tomcat 8 Maven Plugin for Java 8
I am able to build my project and use the tomcat plugin to deploy to the tomcat server (or manually drag the war file in the webapps folder).
Problem is that when I have the following two files:
student-form.html
<html>
<head><title>Student Registration Form</title></head>
<body>
<form action="student-response.jsp">
First name: <input type="text" name="firstName" />
<br/>
Last name: <input type="text" name="lastName" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
student-response.jsp
<html>
<head><title>Student Confirmation Title</title></head>
<body>
The student is confirmed: <%= request.getParameter("firstName")%> ${param.lastName}
</body>
</html>
The request.getParameter method works, but the ${param.lastName} does not and it simply shows up as plain text in the browser: ${param.lastName}.
Using Eclipse (without Maven) it does work for both, so I'm wondering what I'm doing actually different/wrong here and why it is not working.
Thanks in advance for your help.
Once I found out that the references between the braces are called the "Expression Language", I decided to do another search on stackoverflow and found this:
Expression Language in JSP not working
Changing the web.xml as described in the link above made it work.
Thank you all for reading my question, the answer was quite simple actually.

IntelliJ IDEA: Webjars / Maven / Spring MVC - How to correctly import dependencies and have them work?

So I'm beginning Java Web development and I'd just like to have some libraries imported to my IntelliJ project correctly through use of Maven. I've stumbled upon making use of Webjars in order to import bootstrap and jquery, but I am having a great deal of trouble getting anything to work correctly. Wondering if someone could help out? Any suggestions are appreciated.
My project currently has Maven and Spring MVC as supported frameworks. My pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>JavaEEHelloWorld</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>LATEST</version>
<exclusions>
<exclusion>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>
</project>
My spring-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
</beans>
My index.jsp:
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Awesome site.</title>
<link rel="stylesheet" href="/webjars/bootstrap/3.3.5/css/bootstrap.min.css"/>
<script src="/webjars/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="/webjars/jquery/3.0.0-alpha1/jquery.min.js"></script>
</head>
<body>
Hello World!
</body>
</html>
If I try adding the following line to my spring-config.xml, I get this error:
More screenshots:
My project explorer which for some reason never created subdirectories for bootstrap or jquery:
The errors I'm seeing at runtime:

Categories