So I have this web application which has a mysql bd connected to it, it was originally develoded on a simple web-application base but then I wanted to include spring security so I had to refactor the project on a spring boot base and this in when the thing went wrong
this is my index.jsp:
<%@ page import="com.examenjsp.jsp.jdbcConnect"%>
<html>
<link rel="stylesheet" href="style.css">
<body>
<%
String querry = "SELECT * FROM employees";
String url = "jdbc:mysql://localhost:3306/employeesdb";
String name = "thename";
String password = "thepassword";
out.println("<h3>The table is: </h3>" + jdbcConnect.GetTable(querry,url,name,password));
%>
<button id="revealButton">Add User</button>
<div id="hiddenForm" style="display:none;">
<form action="action_page.jsp" method="post">
<label for="first_name">First Name:</label>
<input type="text" id="first_name" name="first_name" required><br>
<label for="last_name">Last Name:</label>
<input type="text" id="last_name" name="last_name" required><br>
<label for="salary_pm">Salary (per month):</label>
<input type="number" id="salary_pm" name="salary_pm" required><br>
<label for="hire_date">Hire Date (YYYY-MM-DD):</label>
<input type="date" id="hire_date" name="hire_date" required><br>
<label for="company">Company:</label>
<input type="text" id="company" name="company" required><br>
<label for="department">Department:</label>
<input type="text" id="department" name="department" required><br>
<input type="submit" value="Submit" >
</form>
</div>
<form action="sort_table.jsp">
<input type="submit" name="Sort" value="Sort table by">
<select name="sortby" id="sort by">
<option value="employee_id">Id</option>
<option value="first_name">First Name</option>
<option value="last_name">Last Name</option>
<option value="salary_pm">Salary</option>
<option value="hire_date">Date</option>
<option value="company">Company</option>
<option value="department">Department</option>
</select>
</form>
<script>
document.getElementById('revealButton').addEventListener('click', function() {
var hiddenForm = document.getElementById('hiddenForm');
if (hiddenForm.style.display === 'none' || hiddenForm.style.display === '') {
hiddenForm.style.display = 'block';
} else {
hiddenForm.style.display = 'none';
}
});
</script>
</body>
</html>
and also a jdbcConnect.java file to handle all the database logic:
The folder has a lot of text so I will include just the part related to other .jsp pages
public static String GetTable(String sqlQuery, String url, String username, String password) {
StringBuilder data = new StringBuilder();
data.append("<table border='1'>");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(url, username, password);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sqlQuery);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
data.append("<tr>");
for (int i = 1; i <= columnCount; i++) {
data.append("<th>").append(rsmd.getColumnName(i)).append("</th>");
}
data.append("<th>Actions</th>");
data.append("</tr>");
while (rs.next()) {
data.append("<tr>");
for (int i = 1; i <= columnCount; i++) {
data.append("<td>").append(rs.getString(i)).append("</td>");
}
data.append("<td><form action='WEB-INF/jsp/edit_employee.jsp' method='post' style='display:inline;'>");
data.append("<input type='hidden' name='employee_id' value='").append(rs.getString("employee_id")).append("'>");
data.append("<input type='submit' value='Edit'>");
data.append("</form>");
data.append("<form action='delete_employee.jsp' method='post' style='display:inline;'>");
data.append("<input type='hidden' name='employee_id' value='").append(rs.getString("employee_id")).append("'>");
data.append("<input type='submit' value='Delete'>");
data.append("</form></td>");
data.append("</tr>");
}
data.append("</table>");
con.close();
} catch (Exception e) {
e.printStackTrace();
}
return data.toString();
}
This is my pom.xml structure:
<?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>3.3.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.examenjsp</groupId>
<artifactId>ExamenJSP_Spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>ExamenJSP_Spring</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>22</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<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.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>10.1.18</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
And here is my SpringApp file:
package com.examenjsp.jsp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.bind.annotation.PostMapping;
@SpringBootApplication
public class ExamenJspSpringApplication {
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(ExamenJspSpringApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(ExamenJspSpringApplication.class, args);
}
@PostMapping("/test-request")
public ResponseEntity<String> testPostRequest() {
return ResponseEntity.ok("POST request successful");
}
@Bean
public InMemoryUserDetailsManager userDetailsService() {
UserDetails user = User.withUsername("user")
.password(encoder().encode("userPass"))
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
@Bean
public PasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.authorizeRequests(authorizeRequests -> authorizeRequests.anyRequest()
.permitAll())
.csrf(AbstractHttpConfigurer::disable);
return http.build();
}
}
Previously, it gave me error 403: forbidden so I tried to fix it by using the official guide https://www.baeldung.com/java-spring-fix-403-error but it turned my 403 error into the 404 error. I even tried to ask gpt but with no success, please help me if you can, I would highly appreciate this.
Balta Cristian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.