How to pass a string between two jsp pages? - string

I want to pass a string(username) between my two jsp pages,
Login.jsp and Details.jsp.
Please tell how it can be done?
I tries this in Login.Jsp
<html>
......
<%
session.setAttribute("key","value");
%>
.....
</html>
In Details.jsp
<html>
......
<%
session.getAttribute("key");
%>
.....
</html>
It showed "NULL" as output.

Usually after a login you should save the user data in sessions:
session.setAttribute("key","value");
and then access from other pages with
session.getAttribute("key");
if you need more information there's a lot around: http://www.jsptut.com/sessions.jsp

Try this
jsp1.jsp
request.setAttribute("name",somevalue);
RequestDispatcher dispatcher = request.getRequestDispatcher("jsp2.jsp");
if (dispatcher != null){
dispatcher.forward(request, response);
}
jsp2.jsp
out.println(request.getAttribute("name"));

There are several ways to pass data from one webpage to another:
Put a form on Login.jsp and let it post to Details.jsp. This will post the values in the form to Details.jsp.
Redirect to Details.jsp?username=ARJUN. This will pass a variable to Details.jsp in te query string.
Put the username in a cookie. The cookie will be submitted to Details.jsp (and every other page), which makes it possible to determine the username in every page.
Put the username in the session. Similar to a cookie, but the session is stored on the server and associated with the user that is currently viewing your website.

First create servlet in write this code:
Here we can get the value using requet.getParameter("name"); here, name is textbox name of a previous page
<%! String name=request.getParameter("name")%>
<% out.println("Welcome :"+name) %>

Related

How to render express views dynamically

I'm new to express JS and try to build CMS like this:
Users have a page builder, where they can drag-and-drop different components on the page.
each component has its own data which also is defined by a user
Each component has its own view template
So, I have to check what components have to load, prepare data for each of them, send this data to an appropriate template, render one big HTML and send to the client.
I know, It's too complicated to explain how to build this, but any tutorials, examples, resources would be appreciated.
IIUC, you can accomplish this using the include function that most template languages have. For the example, I'll use ejs. Also, I'm assuming you know how to get the data for user selections to your server.
on your app.js:
app.get('/someRoute', function(req, res) {
var data = //get user data here
res.render('someTemplate', {data:data});
});
someTemplate.ejs:
<%- include('header') %> //you should do this on every page
<% if (data == 'foo') { %>
<%- include('foo', {data:data}) %> //note that you can pass data to templates like this
<% } %>
<% if (data == 'bar') %>
<%- include('bar') %>
<% } %>
<% include('footer') %>
foo.ejs:
//some component here
If you want to read more, check the docs.
Hope this helps!
You can use Template Engine for that purpose because it enables you to use static template files in your application. At run time, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. This approach makes it easier to design an HTML page.
Some popular template engines that work with Express are Pug, Mustache, and EJS. The Express application generator uses Jade as its default, but it also supports several others.
Also check this link

Macro to get real current page

Not sure how to label the title of this question. I have a snippet of re-usable content with a Subscribe Icon + Social Icons. This snippet is used in almost every page on the site through a repeater.
What I want to do is to attach the CurrentDocument.DocumentName to the Subscribe Icon in order to know which page people came from, something like this:
Subscribe However, looks like that doesn't work. This string shows in the address bar when click on the link: www.domain.com/subscribe?p={%%20CurrentDocument.DocumentNamePath%20|(user)|(hash)34ce5eaa55a6a6ad89...%} I figure because CurrentDocument is actually referring to the snippet itself, not the real current page displaying in the browser. Could you help?
Is your repeater transformation aspx? If so, use <%# %>, so <%# CurrentDocument.DocumentName %>
If this is an ASCX transformation it is like a web user control, so you can call C# methods, try this
<%# CMS.MacroEngine.MacroResolver.Resolve("{%CurrentDocument.DocumentName#%}")%>
Or you can do linke in this in ACSX transformation:
<script runat="server">
string test = "";
protected override void OnInit(EventArgs e)
{
test = CMS.MacroEngine.MacroResolver.Resolve("{%CurrentDocument.DocumentName#%}");
}
</script>
<h1><%# test%></h1>
Consult MacroResolver class for other methods
P.S. Sorry I didnt check my answer in Kenitco for the frist time. Just make sure you calling MacroResolver from the right name space.

Express.js: Cookie being saved late

I think this is because of the asynchronous nature of Node.js in general.
I am changing a cookie's value, then rendering a new page. However upon rendering the new page, the page shows the just-overwritten contents of the cookie. It's as if the cookie is being saved a step late.
res.cookie('loginid',req.body.name,{maxAge:60000});
res.render('page1');
In page1, I have:
<% if(req.cookies.loginid){ %>
cookie remembered. Hi, <%= req.cookies.loginid %>! <% } %>
Example: Cookie currently has loginid="id1". I set it to id2, then render page1. Then I am sent to page1 and of course, it shows id1. If I repeat the procedure by replacing id2 with id3, page1's contents will show id2, and so on.
I tried doing a callback on the res.cookie(...) function, but nothing was called inside it. It looked like:
res.cookie('loginid',req.body.name,{maxAge:60000}, function(req,res){console.log('test');});
When your template (or any other code for that matter) accesses req.cookies, it will access the cookies that were sent by the client (so the values that were previously set, in a different request, using res.cookie()).
Those cookies are independent of cookies that you are setting using res.cookie(), so using that won't update any values in req.cookies within the same request (you're merely telling Express, when this request is done, please include a Set-Cookie header in the response with this value).
Since you're storing req.body.name as cookie value, you can just use that in rendering your template:
res.render('page1', { loginid : req.body.name });
And in your template:
<% if (loginid) { %>
cookie remembered. Hi, <%= loginid %>! <% } %>
(However, you cannot be sure at the time of rendering that the cookie that you are sending back will actually be accepted by the client, so technically saying cookie remembered is premature).

Use fragments-like views in Symfony2

I have two tables in a database. One of them contains the users of the application, and the other one keeps some medical reports about them in a ManyToOne relationship, so every user can have a random number of medical reports in the info table.
On the left of the screen I want to display a list of the users' names, an easy thing to do. Every time I click on the name of one of them, I get to another page that shows the medical data, and I have to go back to get again the list of the users. However, I'd like to have this info in the same view, so every time I click on a name on the left I get his or her data on the right, and when I click on another user, the info of the previous user disappear and the new is shown. I mean, I want a similar behavior that the old HTML iframes had, or the new Android 4 fragments.
Is this possible in Symfony2/Twig?
Twig is just a template engine, it is parsed on the server side and raw HTML/CSS/JS is returned to the browser, you can't write interactions with the user in Twig.
Symfony is a server-side framework, which means it is parsed on the server side and raw HTML/CSS/JS is returned to the browser, you can't write interactions with users with Symfony.
You need to use a client side script lanuage, like JavaScript. You can create AJAX requests to solve your problem. AJAX requests a url and displays the content of the url on the page. As AJAX is one of the most not-crossbrowser things in JavaScript, it is recommend to use a library like MooTools or jQuery.
I recommend to create a RESTful API for the AJAX requests. Something like /users/{id} should show the user information. For that, create a controller that shows the user data and map it to the /users/{id} route:
<?php
// src/Acme/DemoBundle/Controller/UserController.php
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class UserController extends Controller
{
// ...
/**
* #Route("/users/{id}")
*/
public function showAction($id)
{
// select user by id
$user = ...;
// renders the user data
$this->render('AcmeDemoBundle:User:show.html.twig', array('user' => $user));
}
}
And now you create an AJAX request to that url to get the user data:
<ul>
<li><a class="js-show-user" data-id="1">Joren</a></li>
<li><a class="js-show-user" data-id="2">Wouter</a></li>
<!-- ... -->
</ul>
<div id="js-user-data"></div>
<!-- ... include jquery -->
<script>
jQuery(function($) {
var output = $('#js-user-data');
$('.js-show-user').click(function(e) {
jQuery.ajax({
url: '/users/' + $(this).data('id'), // request the correct url
success: function (result) {
output.html(result); // output the result
},
});
});
});
</script>

Could I make dynamic render rely User Permissions in MVC 2?

I make security system in mvc application. In MVC it must be done by AuthorizeAttribute and roles string via actions methods.
Could i make this stuff: instead of action resolve I want to make view where html parts are hidden depend on current user permission set (For example: save button are not visible if user not Administrator).
Within your views, you can do conditional checks, such as:
<% if (User.IsInRole("Admin")) { %>
An Admin-only link
<% } %>
In partial views, the User property is not exposed, but you can still do it:
<% var user = HttpContext.Current.User; %>
<% if (user.IsInRole("Admin")) { %>
An Admin-only link
<% } %>
Brian - i don't think this is a good idea to 'hide' the admin parts. you basically then just expose to logic to anyone opening the html in 'view source' mode. it would be better to have an admin view and a 'normal' view and just do a case statement in the contoller action to deliver the appropriate view where required (still not the best option, but far better than hiding fields). better still, have a master view that contains partialviews which are only rendered if it's the correct user type etc..
just my 'view' on the topic..
jim
You can do either A or B
a) Create partial views for the various elements that change and do something like
<% if (HttpContext.Current.User.IsInRole("Administrator"))
{
Html.RenderPartial("AdminStuff");
}
else
{
Html.RenderPartial("RegularStuff");
}
%>
b) Set the role in your viewdata/viewmodel and use it in the code (not recommended as the view should really contain no logic)
In the controller
ViewData["Admin"] = HttpContext.Current.User.IsInRole("Administrator");
In the view
<% if ((bool)ViewData["Admin"]) { %>
<!-- Show admin stuff -->
<% } %>
Thanks to all for your answers. I see that view dynamic render is a bad practic in mvc applications. I'm used to think that there can be some libraries or templates.
BTW When i told to my PM that a string with roles is a common pattern he sad "Hard code!!!!". Now I'm designing some WCF service with will be an "Aplication Authoriser" ))).
Everyone here seems to forget that there is css for such stuff. You can do the thing you want very easily, at least I am doing at already, and it's working flawlessly.
Let me give you a simple example
Make sure your operations buttons/regions have defined css classes
css class: MODULE-OPERATION
e.g.
Module User
Operations: Add, Edit, Delete, List
Add User
Whenever changing (adding/updating/deleting) roles, you generate a css file for each role
e.g.
You decide that only administrators can add users so this css is generated
//admin_css.css
.USER-ADD { display: none; }
Everytime the page is opened you check what role the user has, and based on the role, you load the css file in you header. So your gui correlates to the logic you have in your application without so much hassle.

Resources