Java Learning Notes (i)

Source: Internet
Author: User

1. About the similarities and differences between overloading and rewriting (overwrite):

overloading and rewriting are all forms of representation of polymorphism. Overloading is the activity during compilation, and overrides are activities during the run .

Overloading: The method of defining the same name in a class, but the parameter list of the method, the method type and the number of parameters, the parameter order is not the same, but the return value does not act as an overloaded flag, you can modify the visibility;

Rewrite: In the subclass of the inheritance relationship, define a method that is the same as the parent class. A subclass is required to override a method of a base class to have the same method name, parameter type, number of arguments, parameter order, and return value as the parent class method, and the visibility must be greater than or equal to the base class method. The overridden method's access permission should be the same as or higher than the access permission of the overridden method in the parent class.

classstudent{protected intMETHOD1 (intAintb) {return0;}} PublicPeopleextendsstudent{ Public intMETHOD1 (intAintb) {return0};//correct,overriding the parent class method to extend access rights//private int method1 (int a,int b) {return 0};//NOTE: You cannot reduce access rights, so this is wrong//Private Long method1 (int a,int b) {return 0};//error, overriding the parent class method, cannot change the type of the return value//private int method1 (int a,long b) {return 0};/* is correct, the method that overloads itself can have different access and return value types, or you can change the private int to public short */

Attention:

The final modified method can be overloaded, but cannot be overridden;

The constructor method can be overloaded, but cannot be overridden, because the constructor method name must be the same as the class name and the method name is different so it cannot be overridden

Private-decorated member methods cannot be overridden and static-decorated methods cannot be overridden.

9 built-in objects for the 2.JSP script:

Application Config exception out page PageContext Request response session

These 9 built-in objects are instances of the Servlet API interface and can be used directly. Note: The essence of the JSP built-in object is the formal parameter or local variable of the _jspservice () method, so when these objects are called, they are not created and used directly. However, you cannot use it in a JSP declaration, or you will not find a variable.

2.1 Application Object (instance of Javax.servlet.ServletContext)

The Application object represents the Web app itself, and you can use application to manipulate web App-related data, typically with two effects:

2.1.1 Sharing data among multiple JSPs and servlets throughout the Web application;

Application.setattribute ("Counter", String.valueof (++i));//Set a value to SetAttribute application property by counter, This property value is valid for the entire web app. Each time the page is refreshed, the variable i is added
, and is set to the value of the Counter property of application
ServletContext sc = getservletconfig (). Getservletcontext ();//Gets the ServletContext instance of the Web app, The instance is then accessed through the application built-in object in the JSP page
Out.println (Sc.getattribute ("Counter"));

Note: Instead of putting data into application just for JSP, servlet data sharing, you should only put the state data for the Web app into the Application object.

2.1.2 access to the configuration parameters for the Web application;

String Driver = application.getinitparameter ("driver"= application.getinitparameter ("url");   = Application.getinitparameter ("user");   = Application.getinitparameter ("password"== conn.createstatement ();  Create a statement object
ResultSet rs = stmt.executequery ("SELECT * from News_inf");

2.2 Exception object, out object, PageContext object

2.2.1 Exception Object (instance of Throwable)

Errors and exceptions generated in JSP scripts, exception objects are only valid on exception handling pages. When the Iserrorpage property of the page directive in the exception handling page is set to True, the page can access the exception object.

2.2.2 Out Object

The Out object represents a page output stream, which is used to output variables or constant values on the page, usually in lieu of an output expression. ------Out.println ();

2.2.3 PageContext Object

This object mainly contains the following methods:

ServletRequest getrequest (): Gets the Request object, in the same vein, you can change the request to Resoponse

HttpSession getsession (): Get Session Object

ServletConfig getservletconfig (): Get Config Object

ServletContext getservletcontext (): Get Application Object

2.3 Request Object

Each request object encapsulates a user request, and all requests parameters are encapsulated in the request object.

The 2.3.1 Request object can be used to obtain the requested header or request parameters, and can also be used to manipulate the properties of the requests scope.

<%Enumeration<String> Headernames =Request.getheadernames (); //Get the name of all request headers  while(Headernames.hasmoreelements ()) {String Headernames=headernames.nextelements ();//get each request and its corresponding valueOUT.PRINTLN (Headername +--+ request.getheader (headername) + "<br/>");} Request.setcharacterencoding ("GBK"); String name= Request.getparameter ("name"); //string getparameter (String paramname): Used to get the value of the paramname request parameter String Gender= Request.getparameter ("Gender"); String National= Request.getparameter ("Country"); string[] Color= Request.getparametervalues ("Color");%>Name:<%=name%>Gender:<%=gender> for(String c:color) {out.println (c+" ");} %>Country:<%=national%>

2.3.2 can be used to manipulate the properties of the request scope:

The httpserveletrequest contains the following two methods for setting and getting the properties of the request scope:

SetAttribute (String attname,object attvalue): Set the AttName to the properties of the request scope

Object getattribute (String attname): Gets the properties of the request scope

2.3.3 perform forward or include:

The Httpserveletrequest class provides a getrequestdispatcher (String path) method, which is the target path of the desired forward or include (note that the target path must begin with "/" ):

Forward (ServletRequest request,servletresponse response): Execution forward

Include (ServletRequest request,servletresponse response): Execute include

Getrequestdispatcher ("/login.jsp"). Include (request, response); // Include the login.jsp page to this page getrequestdispatcher ("/login.jsp"). Include (request, response); // request forward to login.jsp page

Java Learning Notes (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.