" tag to call this event to realize the jsp page calling servlet."/> " tag to call this event to realize the jsp page calling servlet.">

Home  >  Article  >  Java  >  How to call servlet in jsp

How to call servlet in jsp

(*-*)浩
(*-*)浩Original
2019-05-11 11:22:3713732browse

The method for jsp to call servlet: first write an event to call servlet in JS; then use the "onload" of the "" tag to call this event to realize the jsp page calling servlett.

How to call servlet in jsp

How to use this article: First write an event to call the servlet in JS (you can use ajax), and then use the onload call of the

tag this incident.

To carry out the purpose of calling servlet on jsp page.

Recommended course: Java Tutorial.

The code is as follows:

The jsp file code is as follows:

nbsp;html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<meta>
<title>Insert title here</title>
        <script></script>  
        <script></script>    
        <link>    
        
      
      
        <!-- 3.0 -->  
        <link>  
        <script></script>  
     
     
      
        <!-- 2.3.2  
        <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">  
        <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.js"></script>  
        -->  
        <script>  
			function a(){
				$.ajax({  
            		url:"LoaddataServlet",//servlet文件的名称
            		type:"GET",
            		success:function(e){
            			alert("servlet调用成功!");
            		}
            	});
				
			}
        </script>  



The servlet file code is as follows:

package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoaddataServlet
 */
@WebServlet(urlPatterns = "/LoaddataServlet",loadOnStartup = 1)
public class LoaddataServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoaddataServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}


Result picture:

How to call servlet in jsp

The above is the detailed content of How to call servlet in jsp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Why was jsp eliminated?Next article:Why was jsp eliminated?