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>

No comments: