expression language in jstl if statement not working - eclipse

I am trying to understand why this piece of code never prints the method request when run on tomcat 9:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
<title>Add Course</title>
</head>
<body>
<c:if test="${\"POST\".equalsIgnoreCase(pageContext.request.method) && pageContext.request.getParameter(\"submit\") != null}">
<%= request.getMethod() %>
</c:if>
<form method="post">
Name: <input type="text" name="name"> <br>
Credits : <input type="text" name="credits"> <br>
<button type="submit" name="submit">Add</button>
</form>
</body>
</html>

I don't have Tomcat 9 currently installed on my system, but it runs fine in Tomcat 8.5, and Jetty 9.4.17 printing "POST" whenever I post the form.
However, el language, despite resembling Java in many aspects and being able to call Java functions is NOT actually Java. You will encounter more success and consistency using it in a more idiomatic way. In your case, I'm pretty sure the following will work on any up-to-date JSP server:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
<title>Add Course</title>
</head>
<body>
<c:if test="${'post' eq fn:toLowerCase(pageContext.request.method) and param.submit ne null}">
${pageContext.request.method}
</c:if>
<form method="post">
Name: <input type="text" name="name" /><br />
Credits: <input type="text" name="credits" /><br />
<button type="submit" name="submit">Add</button>
</form>
</body>
</html>
Using the fn taglib to compare strings in lower case
Using single quotes around strings to avoid escaping
Using el eq and ne operators for "equals" and "not equals"
Using el and boolean operator
Using el variable param to access request parameters
Using ${...} syntax to output values rather than scriptlets

Related

Cannot complete authorization

I've got LinkedIn API app created and I'm able to get to the 'Allow' screen. That redirects to a form on my site which is submitted to linkedin.com to complete the authorization.
Instead of redirecting to my redirect url I'm getting an error saying:
"You must be authenticated to access this page".
Below is the form I'm submitting except that I've replaced the form field values with fakes.
I've made sure the real redirect URL is authenticated in my LinkedIn application.
What am I doing wrong?
<!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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SoftLink Systems LinkedIn Auth Completion</title>
</head>
<body>
<form id="frmLNAuthComplete" action="https://www.linkedin.com" enctype="application/x-www-form-urlencoded" method="post" >
<input type="hidden" name="grant_type" value="authorization_code" />
<input type="hidden" name="code" value="FAKE_CODE_Gdy1R3cW2hFACX2dV39QwfofGVXg6v1nPH0O_Em4UnzEaR06ZXDGO57HU0DukpehS-EGu-AzF3L8SG8XfrB6oYRkpPtY3cLc" />
<input type="hidden" name="redirect_uri" value="http://linkedin.fake-domain.com/index.php" />
<input type="hidden" name="client_id" value="fake-id-k66l" />
<input type="hidden" name="client_secret" value="fake-secret-rI2mxlqb" />
</form>
<script type="application/javascript">
var frm = document.querySelector("#frmLNAuthComplete");
frm.submit();
</script>
</body>
</html>

Image is not displayed in html inside jsp

I am trying to display a image in html in JSP script, just using img tag.
index.jsp code:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div id="topbar">
<img src="images/x.png" alt="img" height="30" width="25">
</div>
</body>
</html>
I ma using eclipse neon (apache-tomcat local server in Ubuntu) and my directory structure is :
When I run index.jsp , image is not displayed. What I am doing wrong?
Please ask me if you need more information.
#user3138997 gave me hints:
I just need to write as:
<img src="/projectName/images/x.png" alt="img" height="30" width="25">
And this shows the image.

How to include properly a css file in jsp

I tried this :
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css" />
but the css won't apply. :(
Am i missing something obvious ?
EDIT :
Here is my jsp :
<%# page pageEncoding="utf-8"%>
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Log-in</title>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css" />
</head>
<body>
<div class="login-card">
<h1>Log-in</h1><br>
<form>
<input type="text" name="user" placeholder="Username">
<input type="password" name="pass" placeholder="Password">
<input type="submit" name="login" class="login login-submit" value="login">
</form>
<div class="login-help">
Register • Forgot Password
</div>
</div>
</body>
</html>
Try this:- <jsp:include page="style.css"/>
Using the include directive should do the trick.
<link type="text/css" rel="stylesheet" href="css/style.css"/>
try this, will work. In java it ma

Uncaught reference error lift ajax is not defined

I'm trying to create a Lift chat server. I've taken everything straight from the Lift book that is linked to from Lift's main website. Upon running it, I cannot submit my messages because liftAjax is undefined and causes an error that appears in my chrome console.
Correct me if I'm wrong, but shouldn't Lift generate the liftAjax stuff upon starting up the website? I have a feeling I could import liftAjax myself from some source and it would work, but I don't feel I should have to do this.
This is my index.html file.
<!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><title>Home</title></head>
<body class="lift:content_id=main">
<div id="main" class="lift:surround?with=default;at=content">
<!-- the behavior of the div -->
<div class="lift:comet?type=Chat">Some chat messages
<ul>
<li>A message</li>
<li class="clearable">Another message</li>
<li class="clearable">A third message</li>
</ul>
</div>
<div>
<form class="lift:form.ajax">
<input class="lift:ChatIn" id="chat_in" />
<input type="submit" value="Say Something" />
</form>
</div>
</div>
</body>
</html>
This is what is generated:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<div id="main" xmlns="http://www.w3.org/1999/xhtml">
<!-- the behavior of the div -->
<div id="F156429460218VSI1GX_outer" style="display: inline">
<div id="F156429460218VSI1GX" style="display: inline">
<div xmlns="http://www.w3.org/1999/xhtml">Some chat messages
<ul>
<li></li>
</ul>
</div><script type="text/javascript">
// <![CDATA[
/* JSON Func comet $$ F156429460221QEXVXK */function F156429460221QEXVXK(obj) {liftAjax.lift_ajaxHandler('F156429460221QEXVXK='+ encodeURIComponent(JSON.stringify(obj)), null,null);}
// ]]>
</script></div><script type="text/javascript">
// <![CDATA[
var destroy_F156429460218VSI1GX = function() {}
// ]]>
</script></div>
<div>
<form id="F156429460223BHVYIM" action="javascript://" onsubmit="liftAjax.lift_ajaxHandler(jQuery('#'+"F156429460223BHVYIM").serialize(), null, null, "javascript");return false;">
Note: the error happens on the above line
<input name="F1564294602245ECYAU" id="chat_in" xmlns="http://www.w3.org/1999/xhtml" />
<input value="Say Something" type="submit" xmlns="http://www.w3.org/1999/xhtml" />
</form>
</div>
</div>
I'm just not really sure what could be causing this. I'm just starting to learn Lift so I do not know much. Am I missing something?
If you need more information, such as build.sbt or other files just let me know and I can post them as well.
In my case, I was missing following body part that automatically seems to source liftAjax.
<body class="lift:content_id=main">
<div id="main" class="lift:surround?with=default;at=content">
......
</div>
</body>
But, in another case, sourcing jquery manually would work with JQuery module injected in Boot.scala.
<script id="jquery" src="/classpath/jquery.js" type="text/javascript"></script>
You don't need to manually include the liftAjax function, but you do need to include jquery manually. Lift will not automatically inject a reference to it since it doesn't know what version, minification level, or location you want to use. Try adding:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
To the head section of your template.

Even with ASP.NET 3.5, I am getting "Validation of viewstate MAC failed" error

I found a lost of posts to solve this error: "Validation of viewstate MAC failed". I read that it is a ASP.NET 2.0 error which is also a bug. But I am using VS 2008 with ASP.NET 3.5 SP1. Why this error is coming in this version also?
I am using ASP TextBox controls. Some posts have mentioned that ASP TextBoxes generate this error, so I have set AutoPostBack of each TextBox to False.
How to get rid of this mess?
Code Added Below **
* Default.aspx *
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FlexStock._Default" %>
<!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 runat="server">
<!--<%# Register Assembly="MSCaptcha" Namespace="MSCaptcha" TagPrefix="cc1" %>-->
<link rel="Stylesheet" type="text/css" href="~/Styles/main.css" />
<title>FlexStock#.NET - Gamma Edition</title>
</head>
<body id="LoginPageBody" class="loginPageBodyClass">
<img id="imgProductLogo" src="Styles/stockist_gamma.png" style="text-align:center;text-shadow:black;margin-left:500px" alt="Product Logo"/>
<form id="loginForm" runat="server" class="loginForm" action="./Forms/selectCompany.aspx" >
<div id="divMainLoginPage" runat="server">
<div class="label">
<asp:Label ID="lblUserName" runat="server" CssClass="LabelControls" AssociatedControlID="txtUserName">User Name: </asp:Label>
</div>
<div class="value">
<asp:TextBox ID="txtUserName" runat="server" CssClass="TxtStyle" Width="199px" AutoPostBack="false"></asp:TextBox>
</div>
<div class="label">
<asp:Label ID="lblPassword" runat="server" CssClass="LabelControls" AssociatedControlID="txtPassword">Password: </asp:Label>
</div>
<div class="value">
<asp:TextBox ID="txtPassword" runat="server" CssClass="TxtStyle" TextMode="Password" Width="199px" AutoPostBack="false"></asp:TextBox>
</div>
<div class="label">
<asp:Label ID="lblCaptcha" runat="server" Text="Security Check"></asp:Label>
</div>
<div class="value">
<!-- <cc1:CaptchaControl ID="ccJoin" runat="server" CaptchaBackgroundNoise="none" CaptchaLength="5" CaptchaHeight="60" CaptchaWidth="200" CaptchaLineNoise="None" CaptchaMinTimeout="5" CaptchaMaxTimeout="240" /> -->
</div>
<div class="label">
<asp:Label ID="lblEnterCaptcha" runat="server" Text="Enter Value"></asp:Label>
</div>
<div class="value">
<asp:TextBox ID="txtCaptcha" runat="server" CssClass="TxtStyle" Width="199px" AutoPostBack="false"></asp:TextBox><br /><br />
<asp:Button ID="btnLogin" runat="server" Width="60" Text="Login" CssClass="BtnStyle"/>
</div>
</div>
</form>
<img id="img1" src="Styles/impact_logo.png" style="text-align:right;text-shadow:black;margin-left:800px" />
</body>
</html>
selectCompany.aspx **
<%# Page Title="" Language="C#" MasterPageFile="~/master1.Master" AutoEventWireup="true" CodeBehind="~/Forms/selectCompany.aspx.cs" Inherits="FlexStock.Forms.selectCompany" EnableViewStateMac="false" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<link rel="Stylesheet" type="text/css" href="../Styles/selectCompany.css" />
<link rel="Stylesheet" type="text/css" href="../Styles/main.css" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<form id="frmSelectCompany" enableviewstate="false">
<div id="label" class="label" style="width:300px">
<asp:Label ID="lblSelectCompany" runat="server" Text="Select Company or Create New"></asp:Label>
</div>
<div id="btnCreate" style="text-align:right">
<asp:Button ID="btnCreateNew" runat="server" Text="Create New" CssClass="BtnStyle" />
</div><br />
<div id="divGrid">
</div>
</form>
</asp:Content>
This is a pretty common error to see. It's a security feature in ASP.NET that prevents someone from injecting additional controls onto the page after it has rendered, and posting those controls back to the server. It makes your website harder to hack.
Are you creating any input controls through javascript? If so, don't. Create the controls normally inside the web form and wrap them in a <div style='display:none;'> and display them when you need them.
Are you creating any controls dynamically within C#? - If you are, you must create them inside Page_Init, not Page_Load, otherwise ASP.NET won't recognise them as valid.
You can disable this security measure by setting EnableViewStateMac=False at the top of the aspx page, but I strongly advise against it.