Page 1 of 2 12 LastLast
Results 1 to 20 of 24
Like Tree2Likes

Thread: how to call jsp from html

  1. #1
    anchitanc is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default how to call jsp from html

    hi all,

    i have jsp file and i want to call that by html file so for that i used mapping in web.xml file but i am not getting how to call that jsp from html file

    my html file name is :- test1.html

    Java Code:
    <html>
    <head>
    </head>
    </body>
    <a href="firstjsp"> -- test here is first jsp --</a>
    </body>
    </html>


    my jsp file name is :- test.jsp

    and my web.xml file is

    Java Code:
     <servlet>
      <servlet-name>firstjsp</servlet-name>
      <jsp-file>/test.jsp</jsp-file>
    </servlet>
    
     <servlet-mapping>
     <servlet-name>firstjsp</servlet-name>
     <url-pattern>/firstjsp</url-pattern>
     </servlet-mapping>
    please suggested me ...

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: how to call jsp from html

    Why are you mapping your jsp?
    Just reference it directly?

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: how to call jsp from html

    Spammer reported.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: how to call jsp from html

    Quote Originally Posted by Tolls View Post
    Spammer reported.
    It's a good thing (tm) that the rotating knives machine has a solid transparent lid on top of it; while it's fun to see the fear in the eyes of the spammer just before he's sliced up to small pieces, I guess the sound of him, screaming in terror, would be less pleasant.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    JavaForums's Avatar
    JavaForums is offline Administrator
    Join Date
    Dec 2006
    Posts
    876
    Rep Power
    10

    Default Re: how to call jsp from html

    Quote Originally Posted by JosAH View Post
    It's a good thing (tm) that the rotating knives machine has a solid transparent lid on top of it; while it's fun to see the fear in the eyes of the spammer just before he's sliced up to small pieces, I guess the sound of him, screaming in terror, would be less pleasant.
    Jos, be careful. They may close the site due to violence.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: how to call jsp from html

    Quote Originally Posted by JavaForums View Post
    Jos, be careful. They may close the site due to violence.
    No need to worry; no violence was used: I lured the spammer into the machine telling him there probably was money in there; he voluntarily climbed into the machine, I closed the lid and switched on the machine, that's all; and now he is just a pile of slimey remnants and bloody pulp. ;-)

    kind regards,

    Jos

    ps. I'll clean up the machine later.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    anchitanc is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default Re: how to call jsp from html

    hi tolls

    thanks for reply ...
    but i want to pass some parameter to JSP page
    i am trying to mapping JSP in web.xml because i do not want to expose jsp file name outside.
    thanks in advance.

  8. #8
    JavaForums's Avatar
    JavaForums is offline Administrator
    Join Date
    Dec 2006
    Posts
    876
    Rep Power
    10

    Default Re: how to call jsp from html

    Quote Originally Posted by JosAH View Post
    No need to worry; no violence was used: I lured the spammer into the machine telling him there probably was money in there; he voluntarily climbed into the machine, I closed the lid and switched on the machine, that's all; and now he is just a pile of slimey remnants and bloody pulp. ;-)
    Good idea: Show them an easier way to earn money.. :)

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: how to call jsp from html

    Quote Originally Posted by anchitanc View Post
    hi tolls

    thanks for reply ...
    but i want to pass some parameter to JSP page
    i am trying to mapping JSP in web.xml because i do not want to expose jsp file name outside.
    thanks in advance.
    OK, then go via a servlet since, unless you are simply printing the request contents with no processing, you are doing something with the request parameters.
    anchitanc likes this.

  10. #10
    anchitanc is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default Re: how to call jsp from html

    thanks tolls got it

    i have one more question ..

    form my servlet i am inserting some data in mysql databse and form JSP page i am retrieving those data. but when i refresh my JSP page that time last row of database automatic make duplication.

    thanks

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: how to call jsp from html

    Without seeing code (and I hope you aren't actually doing the retrieve in the JSP) I couldn't say.

  12. #12
    anchitanc is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default Re: how to call jsp from html

    my servlet code

    Java Code:
      String name = request.getParameter("username");
       String pass = request.getParameter("password");
    try{
             conn = db.getConnection("hii");
             pstmt = conn.prepareStatement("insert into login_table (user,pass) VALUES(?,?)");
             	pstmt.setString( 1, user);
             	pstmt.setString( 2, pass);
             	
    	pstmt.executeUpdate();
    
    
    out.println("done");
    
    pstmt.close();
    conn.close();
    	RequestDispatcher view = request.getRequestDispatcher("/jsp/showall.jsp");
        view.forward(request,response);
    
    
      }
    my jsp code
    Java Code:
    <%
    Connection conn;
      ResultSet rs;
      Statement statement;
      DB_Connection db =new DB_Connection();
    try{
             conn = db.getConnection();
    
      statement= conn.createStatement();
      rs = statement.executeQuery("Select * from login_table");
      while(rs.next()){
    %>
      <table border="1" >
    <tr>
      <td width="100"><%=rs.getString(1)%></td>
      <td width="100"><%=rs.getString(2)%></td>
    
    </tr>
    </table>
    
    <%
      }
    statement.close();
    conn.close();
    
    }
    insert complete then control goes to JSP and every time i refresh JSP page that time last entry which i entered entry again and again and show with all data.

  13. #13
    anchitanc is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default Re: how to call jsp from html

    like in database :-
    id name pass
    1 abc abc

    when it refresh JSP page
    id name pass
    1 abc abc
    2 abc abc

    again refresh

    1 abc abc
    2 abc abc
    3 abc abc

  14. #14
    anchitanc is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default Re: how to call jsp from html

    my problem has been solved .. i used "sendRedirect" instead of forward.

    if you have any suggestion then i will like to welcome your suggestion.

    thanks tolls.

  15. #15
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: how to call jsp from html

    Quote Originally Posted by Tolls View Post
    Without seeing code (and I hope you aren't actually doing the retrieve in the JSP) I couldn't say.
    I have a stupid question, since I've just starting learning jsp and database stuff recently: why shouldn't he do the retrieve in JSP?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: how to call jsp from html

    Quote Originally Posted by anchitanc View Post
    my problem has been solved .. i used "sendRedirect" instead of forward.

    if you have any suggestion then i will like to welcome your suggestion.

    thanks tolls.
    That would sort it, as the refresh was doing a resend of the data you sent (ie going to the insert servlet).
    One way to avoid the duplications is to improve your primary key possibly, then catch the duplicate key exception?

    Quote Originally Posted by KevinWorkman View Post
    I have a stupid question, since I've just starting learning jsp and database stuff recently: why shouldn't he do the retrieve in JSP?
    JSPs are intended for displaying data, not processing it.
    It's bad practice to have raw Java code in there and, to be honest, I don't like the idea of data-fetching tags either.
    For raw code, it's a swine to debug and is also harder to read since it is surrounded by tags and all the other HTML/JSP cruft you get.
    FOr data-fetching, well...it's doing it in the wrong place.

    A web-app should consist of a few layers.
    Your Servlet (or equivalent in frameworks like Struts and Spring) that controls the flow of data. Takes the request and sends the relevant bits to your
    Business Layer which crunches the data (insert into databases, makes calls to web services, fetches data from here there and everywhere). It should be possible to lift this layer out from your webapp. It should have no reliance on it. The data that's returned from this layer is the forwarded (by the servlet) to
    The View Layer. This is your JSP. It takes the data in the request and displays it.

  17. #17
    anchitanc is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default Re: how to call jsp from html

    Quote Originally Posted by Tolls View Post



    JSPs are intended for displaying data, not processing it.
    It's bad practice to have raw Java code in there and, to be honest, I don't like the idea of data-fetching tags either.
    For raw code, it's a swine to debug and is also harder to read since it is surrounded by tags and all the other HTML/JSP cruft you get.
    FOr data-fetching, well...it's doing it in the wrong place.

    A web-app should consist of a few layers.
    Your Servlet (or equivalent in frameworks like Struts and Spring) that controls the flow of data. Takes the request and sends the relevant bits to your
    Business Layer which crunches the data (insert into databases, makes calls to web services, fetches data from here there and everywhere). It should be possible to lift this layer out from your webapp. It should have no reliance on it. The data that's returned from this layer is the forwarded (by the servlet) to
    The View Layer. This is your JSP. It takes the data in the request and displays it.

    you mean to say html form send the data and servlet and servlet will pass this data to plain old java class and that class will insert data and query data. in case i want to retrieve bulk data which i want to show using jsp , so how can i implement this idea? by retrieve all data in arraylist and return this arraylist to servlet and then servlet will return this arraylist to jsp ? is that right thing ?

  18. #18
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: how to call jsp from html

    Quote Originally Posted by Tolls View Post
    JSPs are intended for displaying data, not processing it.
    It's bad practice to have raw Java code in there and, to be honest, I don't like the idea of data-fetching tags either.
    For raw code, it's a swine to debug and is also harder to read since it is surrounded by tags and all the other HTML/JSP cruft you get.
    FOr data-fetching, well...it's doing it in the wrong place.

    A web-app should consist of a few layers.
    Your Servlet (or equivalent in frameworks like Struts and Spring) that controls the flow of data. Takes the request and sends the relevant bits to your
    Business Layer which crunches the data (insert into databases, makes calls to web services, fetches data from here there and everywhere). It should be possible to lift this layer out from your webapp. It should have no reliance on it. The data that's returned from this layer is the forwarded (by the servlet) to
    The View Layer. This is your JSP. It takes the data in the request and displays it.
    Okay, that makes perfect sense, thanks a lot. I guess I'm still trying to wrap my head around the concept of a servlet.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: how to call jsp from html

    Quote Originally Posted by anchitanc View Post
    you mean to say html form send the data and servlet and servlet will pass this data to plain old java class and that class will insert data and query data. in case i want to retrieve bulk data which i want to show using jsp , so how can i implement this idea? by retrieve all data in arraylist and return this arraylist to servlet and then servlet will return this arraylist to jsp ? is that right thing ?
    Pretty much. One advantage is that it really forces you to avoid trying to splurge large amounts of data onto a single page.
    Of course things like Ajax change the flow a bit, but then, instead of passing the stuff to a JSP, you're creating some JSON string and sending that to some Javascript to process.

    Quote Originally Posted by KevinWorkman View Post
    Okay, that makes perfect sense, thanks a lot. I guess I'm still trying to wrap my head around the concept of a servlet.
    There are two things to always remember about servlets.
    1. Never store anything in them (except possibly local constants).
    2. Get out of them as quickly as possible.

  20. #20
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: how to call jsp from html

    Quote Originally Posted by Tolls View Post
    There are two things to always remember about servlets.
    1. Never store anything in them (except possibly local constants).
    2. Get out of them as quickly as possible.
    Thanks, I'll keep that in mind. I don't mean to hijack the OP's post, but the concept of where a servlet begins and ends is one of the last things I think I need to get into my brain before I begin to really "understand" the web stuff I've been working on in my spare time. Maybe my confusion comes from my (probably incorrect) approach with tomcat and webapps- I'm not sure where a webapp should begin and end either, so the whole site (including login, uploading, viewing, etc) is mashed into one big webapp. So the code that probably belongs in a separate servlet is also jammed into there with no clear line of what a servlet "is".

    At least I did create a hibernate API, so I don't have a ton of sql embedded directly in my jsp code... at least, not anymore.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 05-16-2011, 02:50 AM
  2. call by value and call by reference in java
    By sandeepsai39 in forum New To Java
    Replies: 7
    Last Post: 08-12-2010, 12:03 PM
  3. How can I include a html file in html textarea?
    By surya_dks in forum New To Java
    Replies: 2
    Last Post: 10-04-2008, 08:20 AM
  4. call executable jar from html/jsp
    By Zahari in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 12-06-2007, 01:00 AM
  5. how to call a JAR FILE from HTML
    By leonard in forum Java Applets
    Replies: 1
    Last Post: 08-05-2007, 07:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •