Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

To insert Java script code in a java class and use the function defined in javascript src attribute  RSS feed

 
srinu pearl
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Need help in including the java script code in the java class and also to use the function defined in the java script "src" attribute in the anchor tag. Finally this anchor tag is stored in a string and we have a special framework that takes this string to display the page.

Usually, we set the URL like "www.google.com" in the string but this time if we store the anchor tag with js function will it work?

Below is the sample java class.

class A {

// Assume this method to be like doPost method
public void xxxx() {

// Need to include java script code - <script type="javascript" src=" https://xxxxxxx"

//Anchor tag uses a js function defined in the "src"

String s = "Anchor tag with js"

//Our framework method to display the page.
createResponse(s);

}
 
Michael Gomez
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi srinu, welcome to java ranch.
I am having a hard time understanding what you want to accomplish.
Are you simply trying to render a web page that includes JavaScript ?
Is there a reason you are not using a JSP ?
Without knowing more about the 'special framework' you are using its hard to advise.
Have you tried doing what you describe?
 
srinu pearl
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael for the response. Sorry, If I am not clear with the question. Below are more details.

Our Presentation layer framework is in such a way that whenever we pass a URL (www.google.com) (a string) to a method like context.putRequestValue(constant.targetURL, strTargetURL), the page gets rendered.

Previously the URL is hard coded. For example, If i know the URL I can access the site without log in. We need to add security using javascript to ensure that if someone knows the URL, it shouldn't be displayed unless there is an authentication. I have everything ready but not sure how to embed them in a java code.

Below is the code which I want to add

For example: If the taget URL, where I want to go is www.gmail.com

public void createResponse {


//   <script type="text/javascript" src="https://XYZ.js"></script>

//<a href="javascript:void(0);" onclick="window.open(getTTOUrl(http://www.gmail.com)); return false;">Go to Target</a>


context.putRequestValue(constant.targetURL, strTargetURL).


}

I need to add the commented two lines in the java code and pass the target URL to the context.putRequestValue(constant.targetURL, strTargetURL).

I tried to store them in a string and it didn't work out.








 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still not clear - pick one

1. Need to get JavaScript into the HTML page I am writing to be executed in the client browser - window opens on client browser, etc etc

2. Need to execute JavaScript ON THE SERVER - very unusual - what good is opening a window on the server?


For 1 - just recall that JavaScript is plain text written into the HTML, no special handling required, just write the text with HTML markup.

Bill


 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srinu pearl wrote:We need to add security using javascript


Red flag! You must keep in mind that JavaScript is executed on the client side and it's easy for the user to disable or even change it using tools like GreaseMonkey. Relying on JavaScript for any kind of security is a Bad Idea.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea what it is you are actually trying to do; but whatever it is, you are not going to be able to "embed" JavaScript (not "java script", it's called "JavaScript") in a servlet.

You really need to back way way up and tell us what you are trying to accomplish, rather than how you are trying to do it.
 
srinu pearl
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My goal is to redirect to URL (http://www.xxx.com?a=qwe23). If this URL is exposed to outside world, anyone who knows it can use it and use the services provided for free without any authentication.

We are connecting from Network A to Network B. The site is hosted on Network B. So when we want to redirect the URL from Network A to Network B, a java script security feature should be implemented.

Can anyone suggest a security measure using the Javascript code in the servlet. The thing which I am aware is sending the post request by hiding the parameter. But I wan't to implement the security feature in such a way that anyone who knows the URL cannot access the site without authentication.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srinu pearl wrote:Can anyone suggest a security measure using the Javascript code in the servlet.

As already pointed out, you cannot use JavaScript code from the servlet. And even if you could, that wouldn't do a dang thing to add any "security".

You can make a request to the other server via Java, but know this: unless the request is protected by SSL, anyone on the network can see your request.
 
Michael Gomez
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srinu pearl wrote:My goal is to redirect to URL (http://www.xxx.com?a=qwe23). If this URL is exposed to outside world, anyone who knows it can use it and use the services provided for free without any authentication.

We are connecting from Network A to Network B. The site is hosted on Network B. So when we want to redirect the URL from Network A to Network B, a java script security feature should be implemented.

Can anyone suggest a security measure using the Javascript code in the servlet. The thing which I am aware is sending the post request by hiding the parameter. But I wan't to implement the security feature in such a way that anyone who knows the URL cannot access the site without authentication.


Srinu,

If you want to secure a URL JavaScript is not the way to go. Users can easily disable all JavaScript making your security non-existent.
You mention the URL is nested on Network B, do you access to that server? Look into implementing 'Server side security'. If you only have access to Network A then there's not much you can do other than setting firewall rules to block urls on Network B.
 
srinu pearl
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael.

I have access to only Network A. As you said, Network B should implement some rules or implement some functionality to provide security. I will go with this route.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!