how to use “request” object within a function in jsp

request is accessible inside the scriptlet expressions, because it’s an argument of the method in which these expressions are evaluated (_jspService). But if you want it to be available in your own methods, you must declare it as an argument:

    String fname = request.getParameter("fname");
    String username = getVal("lname", request);
    private String getVal(String param, HttpServletRequest request) {
        return request.getParameter("fname");

Note that you shouldn’t be using scriptlets and getting request parameters in JSPs in the first place. JSPs should be used to generate markup. Do your processing in a servlet/action, prepare the data to be displayed by the JSP by creating and populating beans in the request scope, and then dispatch to a JSP, which should use JSP EL, the JSTL and other custom tags exclusively.