Importing data from a CSV file in a Java Server Page

My recent project involves developing a small login Java program that interacts with servlets and JSPs using the MVC pattern. This program allows users to register and log in to accounts stored in my local MySQL database. To pass values between the servlet and JSP, I am utilizing sessions. Additionally, I have implemented a series of if statements on the servlet for validating inputs.

  • When running the application, the Sign_Up.jsp page opens displaying fields for UserName and Password along with a submit button. There is also a link to Log_In.jsp in the top left corner. Upon entering valid credentials, an account is created in the database and the user is redirected to Welcome.JSP which simply displays some text. Currently, duplicate entries can be created due to lack of uniqueness validation for usernames and passwords.

  • If the link on Sign_UP.jsp is clicked, it redirects to Log_In.jsp where users must enter their credentials. If the credentials exist in the database, they are directed to Welcome.jsp; otherwise, an invalid message prompts them to try again.

-----------------------------------MY NEXT GOAL--------------------------------------

After successfully logging in and being redirected to Welcome.jsp, I aim to incorporate a "Choose File" button on the page. This button should enable users to browse their computer for files, specifically selecting a .csv file for parsing and entry into the database. The .csv file may consist of data like:

Username , Password

UserTest1, 123
UserTest2, 234
UserTest3, 567
UserTest4, 890

Here are my questions related to this process:

  • Can JavaScript be utilized within a JSP for this task?
  • Is it advisable to use JavaScript in a JSP for this purpose?
  • For building a more complex website, would it be recommended to incorporate HTML, CSS, and jQuery code directly in the JSP?

The ultimate goal is to create a website where only the admin can upload a .csv file containing prices for items, which will then be uploaded to the database resulting in updated product stock levels. While I understand there is still much work ahead, this marks a promising start. :)

Answer №1

  1. There is no requirement for JavaScript when it comes to file uploading.
  2. Simply utilize an input of type file without complicating it with JavaScript.
  3. Although you can incorporate JavaScript, it is advised not to embed the code directly into HTML pages. It is better practice to externalize JS code into separate .js files.

A few points to consider regarding the question's title and body:

  • The responsibility of reading a CSV file lies with the controller, not the view.
  • Avoid using sessions to transmit objects from a servlet controller to a view; opt for using the request instead.

Answer №2

JS is unable to read files directly due to restricted access to the filesystem.

To parse a file, it must first be uploaded to the server for processing.

Numerous examples of JSP file uploading can be found online.

The use of the listed technologies/libraries in building complex websites enhances usability and overall aesthetics.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Choose just one button from the variety of buttons available

I am facing an issue with a component that contains multiple cards, each with its own button. Below is the code for this component: import React, { useState, useEffect } from "react"; import "./App.css"; import { Card, Container, Row, C ...

Navigate within the div by scrolling in increments of 100%

I am facing an issue with a div that contains multiple children set to 100% height. My goal is to scroll exactly the height of one child (which is also 100%) on each scroll. However, I am struggling to prevent scrolling multiple steps at a time. I have tri ...

Discover a method to retrieve all recommended strings based on a search query using JavaScript

I have two strings: one containing all the values of countries and the second string that I entered when searching for a country. Now, I am looking to retrieve all results that contain "In", such as India and Indonesia. For example, if I search for "IN" ...

Error: Unrecognized primary operator: $sortby

We have a web application built using Node.js and Mongoose, running on Ubuntu Linux servers hosted on DigitalOcean VPS. One of our Mongoose queries includes a text index with the following operators: less than / equal to limit order by This is how the ...

To proceed, you need to provide the RegId: Error - Unable to locate the file named GCMRegId.txt in the system

I have created a program to incorporate Google Cloud Messaging in android using JSP. I found the reference material here: When attempting to run the program on my server from Eclipse, I encounter a GCMRegId.txt missing error. Here is the code ...

The modal created with Bootstrap 4 is not appearing on the local server, using code sourced from codepen.io

I recently experimented with modal using Bootstrap 4 from a code I found on this website <div class="container"> <h1>Experience Playing YouTube or Vimeo Videos in Bootstrap 4 Modal</h1> <!-- Button trigger modal --> < ...

What causes a device to be rendered unusable when rebooted with an application that limits the use of UserManager.DISALLOW_USB_FILE_TRANSFER?

I followed the Android Developers guide on Lock Task Mode to create a kiosk application. This app automatically starts after the device finishes booting up. However, I encountered an issue when restarting or shutting down the device with this restriction e ...

Discovering additional element information while utilizing the 'Sortable' feature

My Current Objective: In my setup, I have a 'calendar' comprising 5 columns, each column containing 5 separate divs known as timeslots. The main purpose here is to show appointments in these specific timeslots and utilize JQuery's Sortable ...

Group a map in Java based on the values being lists

I am working on a project where I have a map containing user information for different projects. Each project can have multiple users associated with it. Map<String,List<User>>map = new HashMap<>(); map.put("projectA",Arrays.asLi ...

Tips for consolidating multiple strings into one string using jQuery

<span class='st_twitter_large' displayText='Tweet'></span> <span class='st_linkedin_large' displayText='LinkedIn'></span> <span class='st_facebook_large' displayText='Faceboo ...

Retrieve the desired element from an array when a button is clicked

When I click on the button, I need to update an object in an array. However, I am facing difficulties selecting the object that was clicked. For better readability, here is the link to my GitHub repository: https://github.com/Azciop/BernamontSteven_P7_V2 ...

Trouble with switching the <a> tag when utilizing ng-hide/show and a boolean

There is an up arrow that should be hidden using ng-hide if this.showUpArrow is false, and shown using ng-show if this.showUpArrow is true. Initially, this.showUpArrow is set to false when the page loads, and it only switches to true when the top of the pa ...

Determine whether a string includes more than one value from an array of strings

I'm currently developing a program that is designed to determine whether a given string contains multiple words that must appear in a specific order. These words are stored within an array of strings. Here is the progress I have made so far: boolean ...

just half of the four $_POST variables are actually assigned

Here is a simple form structure: <table class='table table-striped table-bordered table-hover' id='dataTables-example'> <thead> <tr> <th>1</th> <th>2</th> ...

Is there a way to exit the page if I enter "hello world" into the Google search bar using Selenium?

Hey everyone, I'm looking to automatically close the browser after typing "Hello World" into the Google search bar using Selenium and Firefox. WebDriver driver = null; public static void main(String args[]) { SimpleSelenium ss = new SimpleSelen ...

Preparing to import JSON file created with the THREE.js editor

var container; var camera; var scene; var renderer; var mesh; var loader; initialize(); function initialize(){ camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.inne ...

"Implementing a Nested V-For Loop with Dynamic Styling

Need help binding an active class @click to a nested item in array of objects. The array looks like this: var arr = [ { "id": 10, "name": "Main Name 1" "parents": { "id": 1, "name": "Name1" } }, { "id": 11, "na ...

The array is arranged properly, yet React is failing to render it in the correct order

Here's the code snippet I am working with: { this.state.rows.map((qc) => qc.BinsByDayByOrchardsQCs.map((qc2) => qc2.BinsByDayByOrchardsQCsDefects.map((qc3) => !defectsArray.includes(qc3.Defect) &am ...

Ways to reuse test cases across different test suites in Protractor

There are some shared test cases that can be utilized across different test suites. For example, suppose suite x and suite y both require the same set of test cases to function properly. To address this, a separate .js file containing the shared code has ...

Can someone explain how to change players when a button is clicked?

There is a single button labeled "Gooi" (meaning "throw" in Dutch). Two players take turns throwing a dice. Player 1 goes first, followed by player 2. I am not sure how to implement this. Should I use a while loop or a for loop to determine whose turn it i ...