Tuesday, September 6, 2016

Call a Java Servlet from JSP and pass a parameter to the Servlet from JSP

Register the servlet in the web.xml file

In the jsp file access the url to get the passed in parameter and pass it to the servlet

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=windows-1252"%>
<html>
    <head>File Upload</head>
    <body>
        <form enctype='multipart/form-data' method='POST' action="/appSubmission/DataUpload">
            <p>
                <input type="file" name="test"/>
            </p>
            <!--<Parameter myparam: <%= request.getParameter("pUserId") %>  </parameter>-->
            <%

//request.setAttribute("pUserId", "8539");
String first = request.getParameter("pUserId");
session.setAttribute("pUserId",first);
%>
            <!--<p>
                ID: 
                <input type="text" name="p_user_id" value='<%=request.getParameter("pUserId")%>'/>
            </p>-->
            <p>
                <input type="submit" value="upload"/>
            </p>
      </form>
    </body>
</html>

Friday, September 2, 2016

Get Hostname, port no. & protocol from the http Servlet





    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        doPost(req, res);
        System.out.println("APP-DEBUG: " + new Timestamp(date.getTime()) + " Leaving doGet()...");
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        System.out.println("APP-DEBUG: " + new Timestamp(date.getTime()) + " Entering doPost()...");
        // initialize variables and the servlet environment
        int i = 0;
        int lineNumber = 0;
        int errorCount = 0;
        Connection conn = null;
        res.setContentType("text/html");
        HttpSession session = req.getSession();
        System.out.println("Http Session................" + session.getAttribute("pUserId"));
        
        String userId = session.getAttribute("pUserId").toString();
        this.printMessage(req);
}



   public void printMessage(HttpServletRequest req) {

        // initialize variables for page attributes
        // these variables must match those set in apputil.stdhead to
        
         int httpPort = req.getServerPort();
        String serverHost = req.getServerName();
        String httpProtocol = req.getScheme();

System.out.println("httpPort"+httpPort+""+"serverHost "+serverHost+"httpProtocol "+httpProtocol );
}