Java Web Learning (+): Servlet Learning (v)

Source: Internet
Author: User

Get Initialization parameters

When you configure a servlet in Web. XML, or when you configure a servlet in annotations, you can configure some initialization parameters, which in the servlet can be

methods provided by the ServletConfig interface to obtain these parameters.

Example:

Servletdemo:

Package Com.servlet;import Java.io.ioexception;import Java.io.printwriter;import javax.servlet.ServletConfig; Import Javax.servlet.servletexception;import Javax.servlet.annotation.webinitparam;import Javax.servlet.annotation.webservlet;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** * servlet implementation                            Class Servletdemmo */@WebServlet (name= "Servletdemo", urlpatterns={"/servletdemo"}, initparams={ @WebInitParam (name= "username", value= "admin"), @WebInitParam (name= "password", value = "123456")}) public class Servletdemo extends HttpServlet {Private static final long serialversionuid = 1L;Private String username;private String password;Public String GetUserName () {return username;}public void Setusername (String username) {This.username = Username;}Public String GetPassword () {return password;}public void SetPassword (String password) {This.password = password;}/** * @see httpservlet#httpservlet () */public Servletdemo () {super (); TODO auto-generated Constructor stub}/*** @see Servlet#init (servletconfig)*/public void init (ServletConfig config) throws servletexception {This.username = Config.getinitparameter ("username");This.password = Config.getinitparameter ("password");}/*** @see Servlet#destroy ()*/public void Destroy () {TODO auto-generated Method Stub}/*** @see Httpservlet#doget (httpservletrequest request, httpservletresponse response)*/protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {TODO auto-generated Method StubResponse.setcontenttype ("Text/html;charset=utf-8");PrintWriter out = Response.getwriter ();Out.print ("Out.print ("}/*** @see Httpservlet#dopost (httpservletrequest request, httpservletresponse response)*/protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {TODO auto-generated Method StubDoget (request, response);}}

index.jsp page Source:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Operation Result:

Introduction to MVC

MVC pattern: The full name of MVC is the model View Controller, which is a popular design idea in the process of software development, which aims to isolate the model, control

System, view. Is the embodiment of a layered thought.

MVC pattern:

Model1 Model:

Model2 Model:

The Model2 development model of Java Web is the embodiment of NVC thought.

1) The JSP page acts as a view layer

2) servlet is the control layer

3) JavaBean is the model layer

4) DB database layer


Not to be continued ...

Java Web Learning (+): Servlet Learning (v)

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.