Validation of JSP - java

I want to validate the JSP in the JSP itself but it is givin me a nullpointer Exception..My code is given below...
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Login Form</title>
<link rel="stylesheet" href="admincss/style.css">
<!--[if lt IE 9]><script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
</head>
<body>
<%
String username=request.getParameter("txt_username");
String password=request.getParameter("txt_password");
if(username.equals("k") && password.equals("k"))
{
response.sendRedirect("Admin");
}
%>
<section class="container">
<div class="login">
<h1>Login to Web App</h1>
<form method="post" action="admin.jsp">
<p><input type="text" name="txt_username" value="" placeholder="Username or Email"></p>
<p><input type="password" name="txt_password" value="" placeholder="Password"></p>
<p class="remember_me">
<label>
<input type="checkbox" name="remember_me" id="remember_me">
Remember me on this computer
</label>
</p>
<p class="submit"><input type="submit" name="btn_commit" value="Login"></p>
</form>
</div>
<div class="login-help">
<p>Forgot your password? Click here to reset it.</p>
</div>
</section>
<section class="about">
<p class="about-links">
View Article
Download
</p>
<p class="about-author">
© 2012–2013 Thibaut Courouble -
MIT License<br>
Original PSD by Orman Clark
</section>
</body>
</html>
I don't understand the error.....If this is not the right method please suggest me the right one

NullPointerException generally means you tried to call a method or access a field on a variable doesn't refer to any object. The only variables you're using in this file, aside from the predefined request and response, are username and password.
The request.getParameter method returns null if the request doesn't have a parameter with the specified name. So if your request doesn't include a txt_username and/or txt_password parameter, the username and/or password variable will be null.
On the line below, when you call username.equals and password.equals, you'll get the exception if those variables are null. You can't call the method if there's no object to call it on.
You need to add some code to handle the case where username or password are null.

Related

Items retrived form servlet are empty [duplicate]

This question already has answers here:
Calling a servlet from JSP file on page load
(4 answers)
Generate an HTML Response in a Java Servlet
(3 answers)
Closed 2 months ago.
I have this problem where I am trying to get items from an ArrayList from a servlet and display it to a JSP file but when I try to get the variables from the item in the ArrayList, they don't show up.
Here is my JSP file
<?xml version="1.0" encoding="UTF-8" ?>
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# page import="java.util.*"%>
<%# page import="com.product.Product"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<script src="https://kit.fontawesome.com/256fd9e75f.js"
crossorigin="anonymous"></script>
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap#4.1.3/dist/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
crossorigin="anonymous" />
<link rel="stylesheet" href="styles.css" />
<title>Home</title>
<link rel="icon" href="src/ShopLogo.png" type="image/x-icon" />
</head>
<body>
<nav class="navbar fixed-top navbar-expand-md navbar-light"
style="background-color: #f15c55"> <span
class="navbar-brand mb-0 h1">Store</span>
<button type="button" data-toggle="collapse" data-target="#navbarNav"
class="navbar-toggler" aria-controls="navbarNav" aria-expanded="false"
aria-label="Toggle Nav">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active"><a href="ProductLoaderServlet"
class="nav-link">Home</a></li>
<li class="nav-item active"><a href="product.html"
class="nav-link">Top Products</a></li>
<li class="nav-item active"><a href="cart.html" class="nav-link"><i
class="fa-regular fa-cart-shopping"></i></a></li>
</ul>
</div>
</nav>
<div class="image-overlay">
<img src="scr/main/resources/Shop.jpg" alt="" />
<h1 class="centered">Shop</h1>
</div>
<%
ArrayList<Product> products = (ArrayList) request.getAttribute("products");
for (Product product : products) {
%>
<div class="cards">
<div class="card" style="width: 18rem">
<img class="card-img-top" src="<%product.getImgUrl();%>" alt="Image" />
<div class="card-body">
<h5 class="card-title">
<%
product.getName();
%>
</h5>
Details
</div>
</div>
</div>
<%
}
%>
</body>
</html>
and this is what I see My View
I assume that I am able to get the items because I can see 5 cards
But I want to know why my variables such as (name, or the image) aren't showing up
Many Thanks for your help

Thymeleaf page is not redirecting to another page

I'm using Spring-boot and thymeleaf and I'm trying to create a registration page but I can't seem to get to register.html from my login page
From the controller I have the following
#Controller
public class LoginController {
private final UserService userService;
#Autowired
public LoginController(UserService userService) {
this.userService = userService;
}
#RequestMapping(value = {"/login"}, method = {GET})
public ModelAndView login() {
return new ModelAndView("/login");
}
#RequestMapping(value = "/register", method = GET)
public ModelAndView register() {
System.out.println("register");
return new ModelAndView("register").addObject(new User());
}
For my login page I have this and it renders properly without an issue
<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Log in</title>
<meta
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"
name="viewport">
<link rel="stylesheet"
href="../static/style/bootstrap/bootstrap.min.css"/>
<link rel="stylesheet"
href="../static/style/font-awesome/font-awesome.min.css"/>
<link rel="stylesheet" href="../static/style/ionicons/ionicons.min.css"/>
<link rel="stylesheet" href="../static/style/adminlte/AdminLTE.min.css"/>
<link rel="stylesheet" href="../static/style/skins/skin-yellow.css"/>
<link rel="stylesheet" href="../static/style/custom.css"/>
</head>
<body class="hold-transition login-page">
<div class="login-box">
<div class="login-logo">
<b>Login</b>
</div>
<div class="login-box-body">
<p class="login-box-msg">Sign in</p>
<form th:action="#{/login}" method="post">
<div class="form-group has-feedback">
<input type="email" class="form-control" placeholder="Email"
name="username"> <span
class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="password" class="form-control" placeholder="Password"
name="password"> <span
class="fa fa-pencil form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<div class="col-md-4"></div>
<div class="col-md-8">
<button type="submit" class="btn btn-primary btn-block btn-flat"
style="margin-bottom: 10px;">Sign In
</button>
</div>
</div>
<br>
<p class="mb-0">
<a th:href="#{/register}">Sign Up</a>
</p>
<p class="mb-0">
<a th:href="#{/forgotPassword}">I forgot my password</a><br>
</p>
</form>
</div>
<div th:if="${param.error != null}">
<div id="error">
<div class="box-body">
<div class="alert alert-danger alert-dismissible">
<button type="submit" class="close" data-dismiss="alert"
aria-hidden="true">×
</button>
<h4>
<i class="icon fa fa-ban"></i>Login Failed!
</h4>
You have entered incorrect credentials please try again
</div>
</div>
</div>
</div>
</div>
<script src="../static/js/jquery/jquery.min.js"></script>
<script src="../static/js/bootstrap/bootstrap.min.js"></script>
<script src="../static/js/adminlte/adminlte.min.js"></script>
</body>
</html>
As I said, it's working fine
For my register page I have this
<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Registration Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="../static/style/bootstrap/bootstrap.min.css"/>
<link rel="stylesheet"
href="../static/style/font-awesome/font-awesome.min.css"/>
<link rel="stylesheet" href="../static/style/ionicons/ionicons.min.css"/>
<link rel="stylesheet" href="../static/style/adminlte/AdminLTE.min.css"/>
<link rel="stylesheet" href="../static/style/skins/skin-yellow.css"/>
<link rel="stylesheet" href="../static/style/custom.css"/>
</head>
<body class="hold-transition register-page">
<div class="register-box">
<div class="register-logo">
<b>Registration</b>
</div>
<div class="card">
<div class="card-body register-card-body">
<p class="login-box-msg">Register a new membership</p>
<form th:object="user" method="get">
<p>Show something</p>
</form>
</div>
</div>
</div>
<script src="../static/js/jquery/jquery.min.js"></script>
<script src="../static/js/bootstrap/bootstrap.min.js"></script>
<script src="../static/js/adminlte/adminlte.min.js"></script>
</body>
</html>
But when I click on <a th:href="#{/register}">Sign Up</a> in my login page, it seems that the page only refreshes and isn't going to the register page, as you can see I tried printing something in the register() method of the LoginContoller but even this isn't printing which tells me my method is never hit, on closer inspection in the browser devtools, I saw that the register page is returning a 302 status as seen in the picture
I tried using a String return type in the controller but that doesn't work either.
I also tried to get the page without any objects being sent to it but that doesn't work.
How do I fix this?
I found the problem, in my SecurityConfig I referenced the wrong page name
.antMatchers(
"/static/**",
"/forgot/*",
"/actuator/*",
"/register")//this was wrong

taking a passcode value from user, i want to use this passcode in url path, i am using thymeleaf as template engine

I have an problem about that taking a passcode value from user, i want to use this passcode in url path, i am using thymeleaf as template engine.
This is my controller
#RequestMapping(value = "/findEvent/{passcode}", method = RequestMethod.POST)
public String findEvent(#PathVariable("passcode") String passcode,
final RedirectAttributes redirectAttributes, Model model) {
Event event=eventService.findByPassCode(passcode);
List<Question> questions=questionService.findQuestionsByPasscode(passcode);
model.addAttribute("questions",questions);
return "questions";
}
and these is my html pages
addEvent.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<form method="post" th:action="#{/eventSave}" th:object="${eventRegister}">
Name:<br>
<input type="text" th:field="*{eventName}"><br>
Passcode:<br>
<input type="text" th:field="*{eventPasscode}"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
passcode.html
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" th:action="#{/findEvent/{passcode}}">
Passcode:<br>
<input type="text" th:text="*{passcode}" ><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
questions.html
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div class="col-12">
<table class="table table-bordered">
<tr>
<th>Question </th>
<th>Votes </th>
</tr>
<tr th:each="questions : ${questions}" th:object="${question}">
<td th:text="*{text} "></td>
<td th:text="*{voteValue} "></td>
<a th:href="#{/voteQuestion/{id} (id=${question.questionId})}">Vote the question</a>
</tr>
</table>
</div>
and this is my result http://localhost:8080/findEvent/%7Bpasscode%7D
If you want the form action URL to contain a value from the form itself, then you need an onSubmit handler to update the URL from the form field.
Form values are only included automatically in the body of a POST request, or as query parameters of a GET request.
Anything else you have to do with JavaScript code.

CloudFlare (Memberful) post requests from Java produce a 403 error

I'm trying to authenticate users using Memberful from a Spring-Boot application. Per the Memberful documentation, the process is as follows:
User logs in at the memberful URL (https://YOURSITE.memberful.com/oauth?client_id=APPLICATION_IDENTIFIER&response_type=code)
User is redirected to my front end (Vue) with a code in the URL provided by Memberful.
The code is passed to my back end server.
Back end server sends a post request using RestTemplate.postForObject(...) from Spring to https://YOURSITE.memberful.com/oauth/token with all required parameters (See snippet below).
At this point an access token payload is expected, but instead a 403 Error is returned.
Here's the full method:
import org.springframework.http.*;
import org.springframework.util.MultiValueMap;
...
private JSONObject getMemberfulAccessToken(String strCode){
String strTargetURL = "https://" + memberfulSubDomain + ".memberful.com/oauth/token";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
parameters.add("client_id", memberfulApplicationIdentifier);
parameters.add("client_secret", memberfulClientSecret);
parameters.add("grant_type", "authorization_code");
parameters.add("code", strCode);
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(parameters, headers);
String strAccessToken = restTemplate.postForObject(strTargetURL, entity, String.class);
return new JSONObject(strAccessToken);
}
And here's the response:
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en-US"> <![endif]-->
<!--[if IE 7]> <html class="no-js ie7 oldie" lang="en-US"> <![endif]-->
<!--[if IE 8]> <html class="no-js ie8 oldie" lang="en-US"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en-US"> <!--<![endif]-->
<head>
<title>Access denied | [My Site].memberful.com used Cloudflare to restrict access</title>
<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1" />
<link rel="stylesheet" id="cf_styles-css" href="/cdn-cgi/styles/cf.errors.css" type="text/css" media="screen,projection" />
<!--[if lt IE 9]><link rel="stylesheet" id='cf_styles-ie-css' href="/cdn-cgi/styles/cf.errors.ie.css" type="text/css" media="screen,projection" /><![endif]-->
<style type="text/css">body{margin:0;padding:0}</style>
<!--[if gte IE 10]><!--><script type="text/javascript" src="/cdn-cgi/scripts/zepto.min.js"></script><!--<![endif]-->
<!--[if gte IE 10]><!--><script type="text/javascript" src="/cdn-cgi/scripts/cf.common.js"></script><!--<![endif]-->
</head>
<body>
<div id="cf-wrapper">
<div class="cf-alert cf-alert-error cf-cookie-error" id="cookie-alert" data-translate="enable_cookies">Please enable cookies.</div>
<div id="cf-error-details" class="cf-error-details-wrapper">
<div class="cf-wrapper cf-header cf-error-overview">
<h1>
<span class="cf-error-type" data-translate="error">Error</span>
<span class="cf-error-code">1010</span>
<small class="heading-ray-id">Ray ID: 515be6c26b80c514 • 2019-09-13 17:39:35 UTC</small>
</h1>
<h2 class="cf-subheadline">Access denied</h2>
</div><!-- /.header -->
<section></section><!-- spacer -->
<div class="cf-section cf-wrapper">
<div class="cf-columns two">
<div class="cf-column">
<h2 data-translate="what_happened">What happened?</h2>
<p>The owner of this website ([My Site].memberful.com) has banned your access based on your browser's signature (515be6c26b80c514-ua21).</p>
</div>
</div>
</div><!-- /.section -->
<div class="cf-error-footer cf-wrapper">
<p>
<span class="cf-footer-item">Cloudflare Ray ID: <strong>515be6c26b80c514</strong></span>
<span class="cf-footer-separator">•</span>
<span class="cf-footer-item"><span>Your IP</span>: 207.200.220.146</span>
<span class="cf-footer-separator">•</span>
<span class="cf-footer-item"><span>Performance & security by</span> Cloudflare</span>
</p>
</div><!-- /.error-footer -->
</div><!-- /#cf-error-details -->
</div><!-- /#cf-wrapper -->
<script type="text/javascript">
window._cf_translation = {};
</script>
</body>
</html>
The fix: Add the JVM option -Dhttp.agent="[Anything you want as the user agent prefix]"
The issue: Cloudflare's Web Application Firewall prevents requests with the User-Agent header Java/1.8.0_172 from being received by Memberful.
Cloudflare's documentation indicates that if you get a 403 response that is not Cloudflare branded, it's the client's (in my case Memberful's) server that's returning the response. That being said, I'm not sure if the response I received in response to a POST request could be considered "Cloudflare branded", but at the time I'm writing this Memberful has told me they haven't applied any custom settings for their environment.
Of course there are other ways to set the User-Agent header value, but this is what I did.
This issue is caused by the browser integrity check, which will run even if the WAF is off. Turn off the browser integrity check with a page rule or globally to avoid this issue.
https://support.cloudflare.com/hc/en-us/articles/200170086-Understanding-the-Cloudflare-Browser-Integrity-Check#:~:text=BIC%20is%20enabled%20by%20default,BIC%20for%20your%20API%20traffic.

script bash or java code to delete in every file html of my folder a tag

hi mate i have in my folder a lot of file html equal( the only difference is the string).
i want in every file html of my folder delete a tag.
an example of my file html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>P0001 Generic DTC: Fuel Volume Regulator Control Circuit/Open</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="P0001 diagnostic trouble code details." />
<meta name="keywords" content="P0001, obd code, obd codes, diagnostic codes, trouble codes, diagnostic trouble codes, ford, gm, toyota, chrysler, dodge, nissan, chevy, dtc, dtcs, engine code, engine codes, check engine light" />
<link rel="stylesheet" type="text/css" href="/static/css/base.css"/>
<link rel="shortcut icon" href="/static/img/favicon.ico" />
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1196991-3']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body class="results one">
<div id="main">
<div id="header">
<div id="logo">DTCSearch.com</div>
<form action="/" method="post">
<p>
<input type="text" id="query" name="query" value="P0001"/>
<input type="submit" id="submit" value=""/>
</p>
</form>
</div>
<div id="content">
<h1>P0001 OBD Trouble Code</h1>
<p>1 result found</p>
<div id="content-body">
<table cellspacing="0" class="one">
<caption>P0001 - Generic</caption>
<tr>
<th>Type</th>
<td>Powertrain - Fuel and Air Metering - ISO/SAE Controlled</td>
</tr>
<tr>
<th>Description</th>
<td><p>Fuel Volume Regulator Control Circuit/Open</p></td>
</tr>
</table>
<p style="font-weight:bold">Try also: http://www.obd-codes.com/p0001</p>
</div>
</div>
<div id="footer">
<div id="footer_banner">
<img src="http://affiliates.eautorepair.net/42/65/15/&dp=84" alt="Do it Yourself Automobile Repair Information" border="0">
</div>
<p>Copyright © 2008–2012 DTCSearch.com<br/>
DTCSearch.com is hosted by ScanTool.net, LLC</p>
</div>
</div>
</body>
</html>`
i want to delete in every file this tag:
<img src="http://affiliates.eautorepair.net/42/65/15/&dp=84" alt="Do it Yourself Automobile Repair Information" border="0">
how do this ? tell me a solution with java or with bash or whit other tecnology.
You can use sed to delete a line from a file.
For example, the command:
sed '/foo/d' myfile
will delete all lines containing the word foo from myfile.
If you have multiple files you can run:
sed -i '/foo/d' *.html
The -i option tells sed to edit the files in-place.
i used:
sed -i 's#<img src="http://affiliates.eautorepair.net/42/65/15/&dp=84" alt="Do it Yourself Automobile Repair Information" border="0">##g' *
now i have a problem more serious. i want to delete
<p>
<input type="text" id="query" name="query" value="XXXXX"/>
<input type="submit" id="submit" value=""/>
</p>
where xxxxx are 5 character different in every file . how can do ?

Categories