Skip to content
Snippets Groups Projects
Commit 67179e73 authored by Razevičius, M. (Marius, Student B-BIT)'s avatar Razevičius, M. (Marius, Student B-BIT)
Browse files

Basically just refactored the previous webapp to match the idea of the...

Basically just refactored the previous webapp to match the idea of the temperature translation webapp. In doing so, changed logic, index.html and also web.xml. Tested on my machine, then refactored the JUnit test instance to suit the new conditions. Everything seems to work and runs well, Tomcat initialization included.
parent 2f3932c9
No related branches found
No related tags found
1 merge request!1Basically just refactored the previous webapp to match the idea of the...
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
package nl.utwente.di.bookQuote;
package nl.utwente.di.TemperatureTranslation;
import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/** Example of a Servlet that gets an ISBN number and returns the book price
import java.io.IOException;
import java.io.PrintWriter;
/** Example of a Servlet that gets a temperature value in Celsius
* and returns the translated temperature value in Fahrenheit.
*/
public class BookQuote extends HttpServlet {
public class TemperatureTranslation extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private Quoter quoter;
private Translator translator;
public void init() throws ServletException {
quoter = new Quoter();
translator = new Translator();
}
public void doGet(HttpServletRequest request,
......@@ -24,7 +29,7 @@ public class BookQuote extends HttpServlet {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Book Quote";
String title = "Temperature";
// Done with string concatenation only for the demo
// Not expected to be done like this in the project
......@@ -35,10 +40,10 @@ public class BookQuote extends HttpServlet {
"</HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1>" + title + "</H1>\n" +
" <P>ISBN number: " +
request.getParameter("isbn") + "\n" +
" <P>Price: " +
quoter.getBookPrice(request.getParameter("isbn")) +
" <P>Temperature (in Celsius): " +
request.getParameter("temp") + "\n" +
" <P>Temperature (in Fahrenheit): " +
translator.calculateTemperature(Integer.parseInt(request.getParameter("temp"))) +
"</BODY></HTML>");
}
}
package nl.utwente.di.TemperatureTranslation;
public class Translator {
public int calculateTemperature(int tempInCelsius) {
return (tempInCelsius * 9/5) + 32;
}
}
package nl.utwente.di.bookQuote;
public class Quoter {
public double getBookPrice(String isbn) {
return switch (isbn) {
case "1" -> 10.0;
case "2" -> 45.0;
case "3" -> 20.0;
case "4" -> 35.0;
case "5" -> 50.0;
default -> 0;
};
}
}
package org.example;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
\ No newline at end of file
......@@ -5,7 +5,7 @@
https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
version="6.0">
<display-name>di.bookQuote</display-name>
<display-name>di.TemperatureTranslation</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
......@@ -16,13 +16,13 @@
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>BookQuote Servlet</description>
<display-name>BookQuote</display-name>
<servlet-name>BookQuote</servlet-name>
<servlet-class>nl.utwente.di.bookQuote.BookQuote</servlet-class>
<description>TemperatureTranslation Servlet</description>
<display-name>TemperatureTranslation</display-name>
<servlet-name>TemperatureTranslation</servlet-name>
<servlet-class>nl.utwente.di.TemperatureTranslation.TemperatureTranslation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookQuote</servlet-name>
<url-pattern>/bookQuote</url-pattern>
<servlet-name>TemperatureTranslation</servlet-name>
<url-pattern>/TemperatureTranslation</url-pattern>
</servlet-mapping>
</web-app>
\ No newline at end of file
<!DOCTYPE HTML>
<!-- Front end for the webBookQuote Servlet. -->
<!-- Front end for the TemperatureTranslation Servlet. -->
<html lang="en">
<head>
<title>Web Book Quote Application</title>
<title>Temperature Calculator Application</title>
<link rel="stylesheet" href="styles.css">
<meta charset="UTF-8">
</head>
<body>
<h1>Web Book Quote Application</h1>
<h1>Temperature Calculator Application</h1>
<form ACTION="./bookQuote">
<p>Enter ISBN number: <input TYPE="TEXT" NAME="isbn"></p>
<form ACTION="./TemperatureTranslation">
<p>Enter temperature value (in Celsius): <input TYPE="TEXT" NAME="temp"></p>
<input TYPE="SUBMIT">
</form>
......
package nl.utwente.di.TemperatureTranslation;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/*** Tests the Temperature Translator */
public class TestTranslator {
@Test
public void testTranslation() throws Exception {
Translator translator = new Translator();
int temp = translator.calculateTemperature(30);
Assertions.assertEquals(86, temp, 0.0, "30 Celsius in Fahrenheit");
}
}
package nl.utwente.di.bookQuote;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/*** Tests the Quoter */
public class TestQuoter {
@Test
public void testBook1() throws Exception {
Quoter quoter = new Quoter();
double price = quoter.getBookPrice("1");
Assertions.assertEquals(10.0, price, 0.0, "Price of book 1");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment