2 Replies - 519 Views - Last Post: 13 August 2017 - 06:03 PM

#1 ryandodd  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 11-August 17

How to use a Java Servlet to read from a text file

Posted 11 August 2017 - 06:08 PM

I am creating a Java web application which takes data from a HTML form and saves it to a textfile. I am trying to read the information from this textfile and display it in an HTML page. However I keep getting the error: HTTP Status 405 - HTTP method POST is not supported by this URL. I have spent a lot of time searching the internet for a solution but I am still having problems. Does anyone have any tips?

Here is my HTML form:

<html>
    <head>
        <title>Register Consignment</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form name = "regConsignment" method = "get" action = "ConsignmentServlet" >
            Consignment Number
            <input type="text" name="consignmentNo"> <br>
            
            Customer Name
            <input type="text" name="customerName"> <br>
            
            Collection Address
            <input type="text" name="collectAddress"> <br>
            
            Number of packages
            <input type="text" name="numPackage"> <br>
            
            Delivery Address
            <input type="text" name="delAddress"> <br>
            
            Weight(KG)
            <input type="text" name="weight"> <br>
            
            Cost per KG
            <input type="text" name="costPerKg"> <br>
            
            Total Cost
            <input type="text" name="totalCost"> <br>
            
            Driver
            <input type="text" name="driver"> <br>
            
            Notes
            <input type="text" name="notes"> <br>
            
            Status
            <input type="text" name="status"> <br>
            
            <input type="submit" value="Register">  <br> <br>
            
            <a href="index.html">Back</a>
            </form>
    </body>
</html>



Here is my servlet to save the data to a textfile:

package Servlets;

import java.io.*;
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(urlPatterns = { "/ConsignmentServlet" })

public class ConsignmentServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter pw = response.getWriter();
String consignmentNo;
    consignmentNo = request.getParameter("consignmentNo");
String customerName = request.getParameter("customerName");
String collectAddress;
    collectAddress = request.getParameter("collectAddress");
String numPackage = request.getParameter("numPackage");
String delAddress = request.getParameter("delAddress");
String weight = request.getParameter("weight");
String costPerKg = request.getParameter("costPerKg");
String totalCost = request.getParameter("totalCost");
String driver = request.getParameter("driver");
String notes = request.getParameter("notes");
String status = request.getParameter("status");

try{
File file = new File("Consignment.txt");
FileWriter fstream = new FileWriter(file,true);
    try (BufferedWriter out = new BufferedWriter(fstream)) {
        out.write(consignmentNo+" "+customerName+" "+collectAddress+" "+numPackage+" "+delAddress+" "+weight+" "+costPerKg
        +" "+totalCost+" "+driver+" "+notes+" "+status);
        out.newLine();
    }
    
    if(!file.exists()){
    	   file.createNewFile();
    	}
pw.println("File is created successfully");
}

catch (IOException e){
System.err.println("Error: " + e.getMessage());
}
}
}


This is the page where I want the text displayed:

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form name = "regcustomer" method = "post" action = "ReportServlet" >
            
            <input type="submit" value="View Management Reports"> <br> <br>
            
            <a href="index.html">Back</a>
            </form>
    </body>
</html>


And here is my servlet to read from the textfile:

package Servlets;

import java.io.*;
import javax.servlet.ServletContext;
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(urlPatterns = { "/ReportServlet" })

public class ReportServlet extends HttpServlet {

    /**
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html");

        
        String filename = "Consignment.txt";

        ServletContext context = getServletContext();

        
        InputStream is = context.getResourceAsStream(filename);
        if (is != null) {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader reader = new BufferedReader(isr);
            PrintWriter writer = response.getWriter();
            String text;

            
            while ((text = reader.readLine()) != null) {
                writer.println(text + "</br>");
            }
        }
    }
}


Is This A Good Question/Topic? 1
  • +

Replies To: How to use a Java Servlet to read from a text file

#2 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 563
  • Posts: 2,316
  • Joined: 13-June 14

Re: How to use a Java Servlet to read from a text file

Posted 11 August 2017 - 08:25 PM

The error is telling you that you don't have a handler for POST requests. You need to override the doPost method for those.

Edit: If you need to learn about the HTTP methods, you can read about them here, for example.

This post has been edited by ndc85430: 11 August 2017 - 11:36 PM

Was This Post Helpful? 1
  • +
  • -

#3 ryandodd  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 11-August 17

Re: How to use a Java Servlet to read from a text file

Posted 13 August 2017 - 06:03 PM

I have tried editing my HTML page and replaced the form method "post" with "get":

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form name = "regcustomer" method = "get" action = "ReportServlet" >
            
            <input type="submit" value="View Management Reports"> <br> <br>
            
            <a href="index.html">Back</a>
            </form>
    </body>
</html>


I am no longer getting an error but the text from the textfile is not appearing, I am just getting a blank screen.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1