This commit is contained in:
Imbus 2024-02-08 11:39:34 +01:00
commit d401afc5f9
14 changed files with 967 additions and 0 deletions

View file

@ -0,0 +1,110 @@
package launch;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.catalina.Server;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.WebResourceSet;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.EmptyResourceSet;
import org.apache.catalina.webresources.StandardRoot;
import java.util.logging.*;
public class Main {
private static File getRootFolder() {
try {
File root;
String runningJarPath = Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath().replaceAll("\\\\", "/");
int lastIndexOf = runningJarPath.lastIndexOf("/target/");
if (lastIndexOf < 0) {
root = new File("");
} else {
root = new File(runningJarPath.substring(0, lastIndexOf));
}
System.out.println("application resolved root folder: " + root.getAbsolutePath());
return root;
} catch (URISyntaxException ex) {
throw new RuntimeException(ex);
}
}
private static void configureLogging() throws SecurityException, UnsupportedEncodingException {
Level level = Level.INFO;
java.util.logging.Logger logger = java.util.logging.Logger.getLogger("");
logger.setLevel(level);
Handler[] handlers = logger.getHandlers();
Handler handler;
if (handlers.length == 1 && handlers[0] instanceof ConsoleHandler) {
handler = handlers[0];
} else {
handler = new ConsoleHandler();
}
handler.setFormatter(new SimpleFormatter());
handler.setLevel(level);
handler.setEncoding("UTF-8");
logger.addHandler(handler);
}
public static void main(String[] args) throws Exception {
configureLogging();
File root = getRootFolder();
System.setProperty("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE", "true");
Tomcat tomcat = new Tomcat();
Path tempPath = Files.createTempDirectory("tomcat-base-dir");
tomcat.setBaseDir(tempPath.toString());
//The port that we should run on can be set into an environment variable
//Look for that variable and default to 8080 if it isn't there.
String webPort = System.getenv("PORT");
if (webPort == null || webPort.isEmpty()) {
webPort = "8080";
}
tomcat.setHostname("localhost");
tomcat.setPort(Integer.parseInt(webPort));
File webContentFolder = new File(root.getAbsolutePath(), "src/main/resources/webapp");
if (!webContentFolder.exists()) {
webContentFolder = Files.createTempDirectory("default-doc-base").toFile();
}
StandardContext ctx = (StandardContext) tomcat.addWebapp("", webContentFolder.getAbsolutePath());
ctx.setDocBase(webContentFolder.getAbsolutePath() + File.separator + "static");
//Set execution independent of current thread context classloader (compatibility with exec:java mojo)
ctx.setParentClassLoader(Main.class.getClassLoader());
System.out.println("configuring app with basedir: " + webContentFolder.getAbsolutePath());
// Declare an alternative location for your "WEB-INF/classes" dir
// Servlet 3.0 annotation will work
File additionWebInfClassesFolder = new File(root.getAbsolutePath(), "target/classes");
WebResourceRoot resources = new StandardRoot(ctx);
WebResourceSet resourceSet;
if (additionWebInfClassesFolder.exists()) {
resourceSet = new DirResourceSet(resources, "/WEB-INF/classes", additionWebInfClassesFolder.getAbsolutePath(), "/");
System.out.println("loading WEB-INF resources from as '" + additionWebInfClassesFolder.getAbsolutePath() + "'");
} else {
resourceSet = new EmptyResourceSet(resources);
}
resources.addPreResources(resourceSet);
resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/jsp", webContentFolder.getAbsolutePath() + File.separator + "jsp", "/"));
ctx.setResources(resources);
tomcat.getConnector();
tomcat.start();
System.out.println("Listening to port " + webPort);
Server server = tomcat.getServer();
server.await();
}
}

View file

@ -0,0 +1,14 @@
package model;
import java.util.List;
public class HelloJspViewModel {
private final List<Integer> data;
public HelloJspViewModel(List<Integer> data) {
this.data = data;
}
public List<Integer> getData() {
return data;
}
}

View file

@ -0,0 +1,65 @@
package servlet;
import java.sql.*;
/*
* Class for managing the database.
*/
public class Database implements AutoCloseable {
// If you have the mysql server on your own computer use "localhost" as server address.
private static String databaseServerAddress = "vm26.cs.lth.se";
private static String databaseUser = "<your databse user on vm26>"; // database login user
private static String databasePassword = "<your password>"; // database login password
private static String database = "<your database, normally the same as database user>"; // the database to use, i.e. default schema
private Connection conn = null;
public Database() {
try{
conn = DriverManager.getConnection("jdbc:mysql://" + databaseServerAddress + "/" +
database, databaseUser, databasePassword);
// Display the contents of the database in the console.
// This should be removed in the final version
try(Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from Respondents");
while (rs.next()) {
String name = rs.getString("name");
System.out.println(name);
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public boolean addName(String name) {
String sql = "insert into Respondents (name) values(?)";
try(PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, name);
ps.executeUpdate();
return true;
} catch (SQLException e) {
if(e.getErrorCode()==1062 && e.getSQLState().equals("23000")){
// duplicate key error
System.out.println(name + " already exists in the database");
} else {
printSqlError(e);
}
return false;
}
// Using the try-with-resources syntax, this will call ps.close() automatically
}
private void printSqlError(SQLException e){
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
throw new RuntimeException(e);
}
public void close() throws SQLException {
conn.close();
}
}

View file

@ -0,0 +1,48 @@
package servlet;
/*
* This class provides forms to be displayed to the user
*/
public class FormGenerator {
private String formElement(String par) {
return '"' + par + '"';
}
/*
* Form for requesting user name
*/
public String nameRequestForm() {
String html = "Please enter your name";
html += "<p> <form name=" + formElement("input");
//html += " action=" + formElement(myURL);
html += " method=" + formElement("get");
html += "<p> Name: <input type=" + formElement("text") + " name=" + formElement("user") + '>';
html += "<p> <input type=" + formElement("submit") + "value=" + formElement("Submit") + '>';
return html;
}
/*
* Form for requesting success with respect to four factors
*/
public String projectDataRequestForm() {
String[][] variables = {
{"Met operational performance", "s11"},
{"Met technical performance", "s12"},
{"Met project schedule", "s13"},
{"Stayed on budget", "s14"}};
String html = "<p>Please assess the importance of the following factors (1-10, where 1 is least important)";
html += "<p> <form name=" + formElement("input2");
html += " method=" + formElement("get");
//html += " action=" + formElement(myURL);
for (int i=0; i<4; i++) {
html += "<p> " + variables[i][0];
html += ": <input type="+ formElement("text") + "name =" + variables[i][1] + '>';
}
html += "<p> <input type=" + formElement("submit") + "value=" + formElement("Submit") + '>';
return html;
}
}

View file

@ -0,0 +1,25 @@
package servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
@WebServlet("/HelloJsp")
public class HelloJsp extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
RequestDispatcher requestDispatcher = this.getServletContext().getRequestDispatcher("/WEB-INF/jsp/example.jsp");
req.setAttribute("simple", "example");
req.setAttribute("complex", new model.HelloJspViewModel(List.of(5,4,3,2,1)));
requestDispatcher.include(req, resp);
}
}

View file

@ -0,0 +1,157 @@
package servlet;
import java.io.*;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Survey.
*/
@WebServlet("/Survey")
public class Survey extends HttpServlet {
private static final long serialVersionUID = 1L;
// Define states
private static final int NEED_NAME = 0;
private static final int NEED_PROJECT_DATA = 2;
private FormGenerator formGenerator = new FormGenerator();
/**
* Default constructor.
*/
public Survey() {
}
/*
* Checks first if name includes characters (i.e. is longer than zero characters) and then
* if so if the name is possible to add to
* the database. It is not possible to add an already existing name to the database.
*/
boolean nameOk(Database db, String name){
boolean result = !name.equals("");
if (result)
result = db.addName(name);
return result;
}
/*
* Checks if a value entered as answer is OK. Answers should be between 1 and 10.
*/
boolean valueOk(int value){
return value > 0 && value <11;
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Try with resources to create a connection, will ensure close is always called.
try(Database db = new Database()) {
// Get the session
HttpSession session = request.getSession(true);
int state = 0;
String name;
boolean sessionShouldBeEnded = false;
// Decide which state the session is in
if (session.isNew())
state = NEED_NAME;
else {
state = (Integer) session.getAttribute("state");
}
// Get a writer, which will be used to write the next page for the user
PrintWriter out = response.getWriter();
// Start the page, print the HTML header and start the body part of the page
out.println("<html>");
out.println("<head><title> FANTASTIC WEB APPLICATION </title></head>");
out.println("<body>");
switch (state){
case NEED_NAME: // First state: get user name
name = request.getParameter("user"); // get the string that the user entered in the form
if (name != null) {
if (nameOk(db, name)) {
session.setAttribute("name", name); // save the name in the session
state = NEED_PROJECT_DATA;
out.println(formGenerator.projectDataRequestForm());
}
else {
out.println("That was not a valid name. Maybe it is already taken by someone else.");
out.println(formGenerator.nameRequestForm());
}
}else{ // name was null, probably because no form has been filled out yet. Display form.
out.println(formGenerator.nameRequestForm());
}
break;
case NEED_PROJECT_DATA:
int s11 = 0, s12 = 0, s13 = 0, s14 = 0;
name = (String) session.getAttribute("name");
String s11String = request.getParameter("s11");
String s14String = request.getParameter("s14");
String s12String = request.getParameter("s12");
String s13String = request.getParameter("s13");
if (s11String==null)
out.println(formGenerator.projectDataRequestForm()); // first time
else {
boolean valuesOk = true;
try {
s11 = Integer.parseInt(s11String);
s12 = Integer.parseInt(s12String);
s13 = Integer.parseInt(s13String);
s14 = Integer.parseInt(s14String);
} catch (NumberFormatException e) {
valuesOk = false;
}
valuesOk = valuesOk && valueOk(s11) && valueOk(s12) && valueOk(s13) && valueOk(s14);
// display the next page
if (valuesOk){
int sum = s11 + s12 + s13 + s14;
// This is only to show that it is possible to do something with the values.
// It is of course meaningless to calculate the sum.
out.println("<p> Hello " + name);
out.println("<p> The sum of the values you entered is " + sum);
sessionShouldBeEnded = true;
} else {
out.println("The values you entered were not OK");
out.println(formGenerator.projectDataRequestForm());
}
}
break;
}
// Save the state in the session until next time doGet is requested
session.setAttribute("state", state);
// Print the end of the HTML-page
out.println("</body></html>");
if (sessionShouldBeEnded)
session.invalidate();
} catch(SQLException ex) {
// If Database.close for any reason fails.
throw new ServletException(ex);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

View file

@ -0,0 +1,16 @@
<%@ page import="model.HelloJspViewModel" %>
<html>
<body>
<h2>Hello Jsp, simple data from Servlet: <%=request.getAttribute("simple")%></h2>
<ol>
<%
HelloJspViewModel model = (HelloJspViewModel)request.getAttribute("complex");
for(int el : model.getData()) { %>
<li>
<%=el%>
<% out.print(":" + el); // or as ordinary java code %>
</li>
<% } %>
</ol>
</body>
</html>

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="/css/pure-min.css">
<title>the Book Store</title>
<style>
.main {
padding: 2em;
color: black;
}
</style>
</head>
<body>
<div class="main">
<h1>the Test Site</h1>
Welcome to this Fantastic Web Page.
<p> The following services are available today:
<ul>
<li> <a href = "http://localhost:8080/Survey"> THE SURVEY</a>
</ul>
</div>
</body>
</html>