最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - servlet's response to a jsp page - Stack Overflow

programmeradmin0浏览0评论

Lets say a servlet text.java returns an html content to a jsp page index.jsp.

IN index.jsp

<button onclick="location.href='text'">CLICK</button>

IN text.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
     out.println("<b>HELLO</b>");
    } finally { 
        out.close();
    }
} 

Now we say that servlet responds to the web browser's request, then after clicking on the button why in the url instead of the jsp page the name of the servlet is there and the control is not returned to the jsp page.

Is that only possible with ajax (formelement.innerHTML= ob.responseText()) ? //where var ob = new XMLHttpRequest();

Lets say a servlet text.java returns an html content to a jsp page index.jsp.

IN index.jsp

<button onclick="location.href='text'">CLICK</button>

IN text.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
     out.println("<b>HELLO</b>");
    } finally { 
        out.close();
    }
} 

Now we say that servlet responds to the web browser's request, then after clicking on the button why in the url instead of the jsp page the name of the servlet is there and the control is not returned to the jsp page.

Is that only possible with ajax (formelement.innerHTML= ob.responseText()) ? //where var ob = new XMLHttpRequest();

Share Improve this question edited Aug 4, 2012 at 15:07 devdoe asked Aug 4, 2012 at 9:28 devdoedevdoe 4,3608 gold badges44 silver badges83 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

you can proceed like this...

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    String str = "<b>heloo</b>";
    request.setAttribute("result", str);
    request.getRequestDispatcher("/index.jsp").forward(request, response);
}

and in jsp just get the result by :

request.getAttribute("result");

A JSP is a servlet written as a template. Servlets are server-side, and typically do not call each other. This is your web page (wether it was generated through a jsp or not) that exposes a link to a URL, not a java file. When the link is clicked, your browser sends a request to your server for the URL of the link. So on your server this URL (which is up to you to define) has to be mapped to a Servlet class that will handle the request and produce a response. This URL-to-servlet mapping being configured in the web.xml file of your WAR.

you should see how to map servlet in web.xml, search for a basic servlet tutorial.

you cannot give a link like text.java and expect it will fire the text.java servlet. you need to map a url to servlet class

发布评论

评论列表(0)

  1. 暂无评论