Asked  7 Months ago    Answers:  5   Viewed   30 times

What is the difference between getAttribute() and getParameter() methods within HttpServletRequest class?

 Answers

97
  • getParameter() returns http request parameters. Those passed from the client to the server. For example http://example.com/servlet?parameter=1. Can only return String

  • getAttribute() is for server-side usage only - you fill the request with attributes that you can use within the same request. For example - you set an attribute in a servlet, and read it from a JSP. Can be used for any object, not just string.

Tuesday, June 1, 2021
 
millenomi
answered 7 Months ago
62

Intention of ConstraintLayout is to optimize and flatten the view hierarchy of your layouts by applying some rules to each view to avoid nesting.

Rules remind you of RelativeLayout, for example setting the left to the left of some other view.

app:layout_constraintBottom_toBottomOf="@+id/view1"

Unlike RelativeLayout, ConstraintLayout offers bias value that is used to position a view in terms of 0% and 100% horizontal and vertical offset relative to the handles (marked with circle). These percentages (and fractions) offer seamless positioning of the view across different screen densities and sizes.

app:layout_constraintHorizontal_bias="0.33" <!-- from 0.0 to 1.0 -->
app:layout_constraintVertical_bias="0.53" <!-- from 0.0 to 1.0 -->

Baseline handle (long pipe with rounded corners, below the circle handle) is used to align content of the view with another view reference.

Square handles (on each corner of the view) are used to resize the view in dps.

enter image description here

This is totally opinion based and my impression of ConstraintLayout

Monday, June 7, 2021
 
relipse
answered 7 Months ago
23

All the support libraries are dropping the v4 v7 v12 v13 etc tags and everything is refactored into the androidx packages.

They are essentially the same but for future reference androidx will be the library that we should use in our apps.

Android studio 3.2 canary that comes out this week (week of May 14, 2018) should have the tool that allows automatic refactoring to the androidx packages. There was an announcement about this at google i/o 2018.

Sunday, July 18, 2021
 
Amber
answered 5 Months ago
31

I'll try to explain it correctly in the most simple way (as addition to the correct @Nathan Hughes answer) :

From the perspective of HTML vs JAVA code, servlet is more like HTML wrapped with JAVA. It gives a strong support for handling application layer in the multilayered architecture. On the other hand JSPs were created to support the creating of presentation layer. The init() method of a servlet is called only once during initialization of the servlet.

So the first point : Servlets were here before JSPs.

Now to JSPs. Again from the perspective of HTML and JAVA, JSP is more like JAVA wrapped with HTML.

WEB CONTAINER performs a translation of the JSP "source code" to the equivalent Servlet java code. This translated java Servlet source code is then compiled and the WEB CONTAINER handles the realisation of the Servlet. Simply:

MyPage.jsp --> (translate) --> MyPage_jsp.java --> (compile) --> MyPage_jsp.class --> (load) --> Java Servlet

The jspInit() method is called by WEB CONTAINER as a part of the initialization phase of the JSP lifecycle.

So to your question : jspInit() not equals init().

Thursday, August 5, 2021
 
Raef
answered 5 Months ago
40

If you look at the top of generated jsp class, you will see the following line

 private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();

Now, one possible solution to customize out object is to have a custom JspFactory implmentation.

Steps

Create a custom JspFactory implementation

public class MyJspFactory extends JspFactory {
    private static JspFactory _myFactory = null;
    public MyJspFactory(JspFactory factory) {
        _myFactory = factory;
    } 
   //All abstract methods which looks up _myFactory and does the same thing

   public PageContext getPageContext(Servlet servlet, ServletRequest request, ServletResponse response, String errorPageURL, boolean needsSession, int bufferSize, boolean autoflush) {
        PageContext myCtxt = _myFactory.getPageContext(....)
        //Create a customPageContext and wrap myCtxt in it and return
   }
}

Create a CutsomPageContext class

public class MyPageContext extends PageContext {
    private PageContext _ctxt = null;

    public void setPageContext(PageContext ctxt) {
        _ctxt = ctxt;
    }

    //Implement all abstract methods using _ctxt object

    @override
    public  JspWriter getOut() {
        JspWriter _out = _ctxt.getOut();

        //Wrap _out object using MyJSPWriter as mentioned in question and return back;

    }
}

Now during the init face of the servlets, add the following lines

JspFactory newFactory = new MyJspFactory(JspFactory.getDefaultFactory());
JspFactory.setDefaultFactory(newFactory);   

I have not tried it out. But conceptually it should work. Please let us know if you could achieve what you wanted through this.

Good Luck!

Thursday, September 23, 2021
 
Torxed
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share