• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Having trouble with Filters - I cannot get this code to work. It compiles but doesn't run correctly.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am basically using a MVC design pattern. It's simple, but there is one thing I cannot get to work.

The code for my DD is as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<servlet>
<servlet-name>Ch3 Beer</servlet-name>
<servlet-class>com.example.web.BeerSelect</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Ch3 Beer</servlet-name>
<url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>

<security-constraint>
<web-resource-collection>
<web-resource-name>BeerSelect</web-resource-name>
<url-pattern>/SelectBeer.do</url-pattern>
<http-method>POST</http-method>
</web-resource-collection>

<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>

<security-role>
<role-name>admin</role-name>
</security-role>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/WEB-INF/loginPage.html</form-login-page>
<form-error-page>/WEB-INF/loginError.html</form-error-page>
</form-login-config>
</login-config>


<filter>
<filter-name>BeerRequest</filter-name>
<filter-class>com.example.web.BeerRequestFilter</filter-class>
<init-param>
<param-name>LogFileName</param-name>
<param-value>UserLog.txt</param-value>
</init-param>
<filter-mapping>
<filter-name>BeerRequest</filter-name>
<url-pattern>/SelectBeer.do</url-pattern>
</filter-mapping>

</filter>
</web-app>




Now the code for my servlet (The controller is):

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.example.web;

import com.example.model.*;
import java.util.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;

/**
*
* @author Aaron
*/
public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String c = request.getParameter("color");
BeerExpert be = new BeerExpert();
List result = be.getBrands(c);

request.setAttribute("styles", result);
RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request, response);
}
}



The code for my model is:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.example.model;
import java.util.*;

/**
*
* @author Aaron
*/
public class BeerExpert {
public List getBrands(String color) {
List brands = new ArrayList();
if(color.equals("amber")) {
brands.add("Jack Amber");
brands.add("Red Moose");
}
else {
brands.add("Jail Pale Ale");
brands.add("Gout Scout");
}
return brands;
}
}


The code for the view is :

<%--
Document : result
Created on : Jun 2, 2016, 12:50:03 PM
Author : Aaron
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ page import="java.util.*" %>
<html>
<body>
<h1 align="center">Beer Recommendations JSP</h1>


<%
List styles = (List)request.getAttribute("styles");
Iterator it = styles.iterator();
while(it.hasNext()) {
out.print("<br>try: " + it.next());
}
%>


</body>
</html>


I have a form based authentication which I know is written correctly but I will copy it just in case:

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<body>
Please login daddy-o
<form action="j_security_check">
<input type="text" name="j_username">
<input type="password" name="j_password">
<input type="submit" value="Enter" />
</form>
</body>
</html>



Here is the code for the LoginError page:

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<body>Sorry dude, wrong password</body>
</html>



And here is the code for the index.html (Where you start):

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<body>
<h1 align="center">Beer Selection Page</h1>
<form method="POST" action="SelectBeer.do">
Select beer characteristics


Color:
<select name="color" size="1">
<option value="light">light</option>
<option value="amber">amber</option>
<option value="brown">brown</option>
<option value="dark">dark</option>
</select>





<center>
<input type="SUBMIT" />
</center>
</form>
</body>
</html>



Here is the code for the filter for authentication:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.example.web;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;

/**
*
* @author Aaron
*/
public class BeerRequestFilter implements Filter {
private FilterConfig fc;

public void init(FilterConfig config) throws ServletException {
this.fc = config;
}

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws ServletException, IOException {
HttpServletRequest httpReq = (HttpServletRequest)req;
String name = httpReq.getRemoteUser();
if (name != null) {
fc.getServletContext().log("User " + name + " is updating");
}
chain.doFilter(req, resp);
}

public void destroy() {

}
}



So my question is every time I try to run this program I get a message after logging on that says "HTTP Status 500 - Unable to find match between the canonical context path [/Beer-V1] and the URI presented by the user agent [r=light1/SelectBeer.do]"

What am I doing wrong?








 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic