Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 data = new HashMap();

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 data = (Map) getServletContext()

.getAttribute( "data" );

response.setContentType( "text/html" );

PrintWriter out = response.getWriter();

out.println(

"Currency Converter" );

out.println( "

" );

out.println( "" );

// use the keys to generate " );

for( String c : data.keySet() )

out.println( "" );

out.println( "" );

out.println( "= ?" );

// use the keys to generate " );

for( String c : data.keySet() )

out.println( "" );

out.println( "" );

out.println( "" );

out.println( "" );

}

@SuppressWarnings("unchecked")

protected void doPost( HttpServletRequest request,

HttpServletResponse response ) throws ServletException, IOException

{

Map data = (Map) getServletContext()

.getAttribute( "data" );

String c1 = request.getParameter( "c1" );

String c2 = request.getParameter( "c2" );

double amount = Double.parseDouble( request.getParameter( "amount" ) );

double result = amount * data.get( c2 ) / data.get( c1 );

response.setContentType( "text/html" );

PrintWriter out = response.getWriter();

out.println(

"Currency Converter" );

out.println(

"

" + amount + " " + c1 + " = " + result + " " + c2 + "

" );

out.println( "

Back

" );

out.println( "" );

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 1 Lnai 6321

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

364215879X, 978-3642158797

More Books

Students also viewed these Databases questions

Question

d. Present your teams conclusions to the class.

Answered: 1 week ago

Question

a. Describe the encounter. What made it intercultural?

Answered: 1 week ago