'Http status 405 - http method get is not supported by this url(Blank Page)
I have a web app that is used as a web online shop. Here customers can log in and search through products and order them.
Admins can review all orders and add/delete products on the app. I have a problem with adding a new product. When I fill out the form on the admin-add-product.jsp page and submit the data, I get redirected to AddProducts servlet, but I only get a blank page and the product isn't added to the database.
admin-add-products.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Kreiranje shopa</title>
<!-- Importing all ui libs -->
<link href="assets/css/bootstrap.css" rel="stylesheet" />
<link href="assets/css/font-awesome.css" rel="stylesheet" />
<link href="assets/css/style.css" rel="stylesheet" />
<link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="all" />
<link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
<script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
<script src="js/simpleCart.min.js"></script>
<script type="text/javascript" src="js/bootstrap-3.1.1.min.js"></script>
<link href='http://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Lato:400,100,100italic,300,300italic,400italic,700,900,900italic,700italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
<script src="js/jquery.easing.min.js"></script>
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css' />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<%
////Provera da li je admin u sesiji ili ne
if (session.getAttribute("uname") != null && session.getAttribute("uname") != "") {
%>
<jsp:include page="adminHeader.jsp"></jsp:include>
<div class="content-wrapper">
<div class="container">
<div class="row pad-botm">
<div class="col-md-12">
<h4 class="header-line">Dodaj proizvod</h4>
</div>
</div>
<%
String message = (String) session.getAttribute("message");
if (message != null) {
session.removeAttribute("message");
%>
<div class="alert alert-danger" id="success">Proizvod uspešno dodat.</div>
<%
}
%>
<div class="row">
<div class="col-md-12">
<div class="panel panel-info">
<div class="panel-heading">Dodaj proizvod</div>
<div class="panel-body">
<form role="form" action="AddProducts" method="post"
enctype="multipart/form-data">
<div class="form-group">
<label>Unesite naziv</label> <input class="form-control" type="text" name="pname" required />
</div>
<div class="form-group">
<label>Cena</label> <input class="form-control" type="number" name="price" required/>
</div>
<div class="form-group">
<label>Opis</label> <input class="form-control" type="text" style="min-height: 100px;" name="description" required/>
</div>
<div class="form-group">
<label>Cena sa PDV-om</label> <input class="form-control" type="number" name="mprice" required/>
</div>
<div class="form-group">
<label>Status</label>
<select class="form-control" name="status" required>
<option>Aktivan</option>
<option>Neaktivan</option>
</select>
</div>
<div class="form-group">
<label>Kategorija proizvoda</label> <input class="form-control" type="text" name="category" required/>
</div>
<div class="form-group">
<label>Dodaj sliku proizvoda</label> <input type="file" name="uploadLogo"/>
</div>
<button type="submit" class="btn btn-success" onclick="return confirm('Da li ste sigurni da želite da dodate ovaj proizvod?');">Dodaj proizvod</button>
<button type="reset" class="btn btn-danger">Poništi</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<jsp:include page="footer.jsp"></jsp:include>
<script src="assets/js/jquery-1.10.2.js"></script>
<script src="assets/js/bootstrap.js"></script>
<script src="assets/js/custom.js"></script>
<%
} else {
response.sendRedirect("admin-login.jsp");
}
%>
</body>
</html>
AddProducts.java
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.connection.DatabaseConnection;
@WebServlet("/AddProducts")
public class AddProducts extends HttpServlet {
//Path where all the images are stored
private final String UPLOAD_DIRECTORY = "C:\\Users\\danilodjurovic\\Pictures";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//Creating session
HttpSession session = request.getSession();
if (ServletFileUpload.isMultipartContent(request)) {
try {
//Taking all image requests
List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
String imageName = null;
String productName = null;
String productQuantity = null;
String productPrice = null;
String descrip = null;
String mrpPrice = null;
String status = null;
String category = null;
//SALTCHARS to generate unique code for product
String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder salt = new StringBuilder();
Random rnd = new Random();
while (salt.length() < 3) { // length of the random string.
int index = (int) (rnd.nextFloat() * SALTCHARS.length());
salt.append(SALTCHARS.charAt(index));
}
String code = salt.toString();
for (FileItem item : multiparts) {
if (!item.isFormField()) {
//Getting image name
imageName = new File(item.getName()).getName();
//Storing in the specified directory
item.write(new File(UPLOAD_DIRECTORY + File.separator + imageName));
//Retriving all information from frontend
FileItem pName = (FileItem) multiparts.get(0);
productName = pName.getString();
FileItem price = (FileItem) multiparts.get(1);
productPrice = price.getString();
FileItem description = (FileItem) multiparts.get(2);
descrip = description.getString();
FileItem mprice = (FileItem) multiparts.get(3);
mrpPrice = mprice.getString();
FileItem fstatus = (FileItem) multiparts.get(4);
status = fstatus.getString();
FileItem pcategory = (FileItem) multiparts.get(5);
category = pcategory.getString();
}
}
try {
int id = 0;
String imagePath = UPLOAD_DIRECTORY + imageName;
//Querying to insert product in the table
int i = DatabaseConnection.insertUpdateFromSqlQuery("insert into tblproduct(id,active,code,description,image,image_name,name,price,mrp_price,product_category) values('" + id + "','" + status + "','" + code + "','" + descrip + "','" + imagePath + "','" + imageName + "','" + productName + "','" + productPrice + "','" + mrpPrice + "','" + category + "')");
//If product inserted sucessfully in the database
if (i > 0) {
String success = "Product added successfully.";
//Adding method in session.
session.setAttribute("message", success);
//Response send to the admin-add-product.jsp
response.sendRedirect("admin-add-product.jsp");
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception ex) {
//If any occur occured
request.setAttribute("message", "File Upload Failed due to " + ex);
}
} else {
request.setAttribute("message", "Sorry this Servlet only handles file upload request");
}
}
}
Any help is appreciated.
Thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
