Question: Having a hard time converting this. Could someone help? Modify the Currency Converter application so it uses the MVC architecture. In particular, Requests should always
Having a hard time converting this. Could someone help?
Modify the Currency Converter application so it uses the MVC architecture. In particular,
Requests should always be sent to controllers, not directly to views.
Servlets do not generate HTML content.
No Java code in JSP, i.e. no JSP expression (<%= %>), JSP scriptlet (<% %>), or JSP declaration (<%! %>).
Current Java Form
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/CurrencyConverter")
public class CurrencyConverter extends HttpServlet {
private static final long serialVersionUID = 1L;
public CurrencyConverter()
{
super();
}
public void init( ServletConfig config ) throws ServletException
{
super.init( config );
Map
try
{
// read file
Scanner in;
in = new Scanner( new File(
getServletContext().getRealPath( "/WEB-INF/rates.txt" ) ) );
while( in.hasNextLine() )
{
String line = in.nextLine();
String tokens[] = line.split( " " );
data.put( tokens[0], Double.valueOf( tokens[1] ) );
}
in.close();
}
catch( FileNotFoundException e )
{
throw new ServletException( e );
}
getServletContext().setAttribute( "data", data );
}
@SuppressWarnings("unchecked")
protected void doGet( HttpServletRequest request,
HttpServletResponse response ) throws ServletException, IOException
{
Map
.getAttribute( "data" );
response.setContentType( "text/html" );
PrintWriter out = response.getWriter();
out.println(
"
out.println( "
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
