Servlet 3.0 does not need to be configured with web. xml, and uses the injection method to configure servlet for login (the server must support servlet3.0), web. xmlservlet3.0

Source: Internet
Author: User

Servlet 3.0 does not need to be configured with web. xml, and uses the injection method to configure servlet for login (the server must support servlet3.0), web. xmlservlet3.0

First, declare the above error, and I don't know what's going on.

Select java EE6.0 when creating a project, and no servlet3.0.

Let's take a look at a basic example.

Test. java is used to Test non-configuration files and no static pages (jsp, html)

Directly access the servlet to obtain information from the server.

Test. java code

1 package com. gys; 2 3 import java. io. IOException; 4 import java. io. printWriter; 5 6 import javax. servlet. servletException; 7 import javax. servlet. annotation. webServlet; 8 import javax. servlet. http. httpServlet; 9 import javax. servlet. http. httpServletRequest; 10 import javax. servlet. http. httpServletResponse; 11 12 @ WebServlet (13 name = "Test", 14 urlPatterns = {"/test"} 15) 16 17 public class Test extends HttpServlet {18 @ Override19 protected void doPost (HttpServletRequest req, HttpServletResponse resp) 20 throws ServletException, IOException {21 doGet (req, resp ); 22} 23 24 @ Override25 protected void doGet (HttpServletRequest request, HttpServletResponse response) 26 throws ServletException, IOException {27 response. setContentType ("text/html; charset = UTF-8"); 28 PrintWriter out = response. getWriter (); 29 out. println ("

Access results:

Read the above name and urlpattern parameters, and continue to look down.

ServletConfigDemo. java code

1 package com. gys; 2 3 import java. io. IOException; 4 import java. util. enumeration; 5 6 import javax. servlet. requestDispatcher; 7 import javax. servlet. servletConfig; 8 import javax. servlet. servletContext; 9 import javax. servlet. servletException; 10 import javax. servlet. annotation. webInitParam; 11 import javax. servlet. annotation. webServlet; 12 import javax. servlet. http. httpServlet; 13 import javax. servle T. http. httpServletRequest; 14 import javax. servlet. http. httpServletResponse; 15 import javax. servlet. http. httpSession; 16 17 @ WebServlet (18 urlPatterns = {"/servletConfigDemo. do "}, 19 loadOnStartup = 1,20 name =" servletConfigDemo ", 21 displayName =" demo ", 22 initParams = {23 @ WebInitParam (name =" success ", value = "success.html"), 24 @ WebInitParam (name = "error", value = "error.html") 25} 26) 27 public class servletConfigD Emo extends HttpServlet {28 @ Override29 protected void doPost (HttpServletRequest request, HttpServletResponse response) 30 throws ServletException, IOException {31 ServletConfig config = getletservconfig (); 32 // 1. getInitParameter (name) Method 33 String success = config. getInitParameter ("success"); 34 String error = config. getInitParameter ("error"); 35 36 System. out. println ("success -----" + success); 37 System. out. pr Intln ("errror ------" + error); 38 39 // 2. getInitParameterNames Method 40 Enumeration enumeration = config. getInitParameterNames (); 41 while (enumeration. hasMoreElements () {42 String name = (String) enumeration. nextElement (); 43 String value = config. getInitParameter (name); 44 System. out. println ("name -----" + name); 45 System. out. println ("value -----" + value); 46} 47 48 // 3getServletContext method 49 ServletContext servletContext = Config. getServletContext (); 50 System. out. println ("servletContext ----" + servletContext); 51 52 // 4. getServletName method 53 String servletName = config. getServletName (); 54 System. out. println ("servletName ------" + servletName); 55 56 request. setCharacterEncoding ("UTF-8"); 57 String userId = request. getParameter ("userId"); 58 String passwd = request. getParameter ("passwd"); 59 60 // judge 61 if (userId! = Null & "gys". equals (userId) & passwd! = Null & "gys ". equals (passwd) {62 HttpSession session = request. getSession (); 63 session. setAttribute ("user", userId); 64 // jump to 65 RequestDispatcher requestDispatcher = request. getRequestDispatcher (success); 66 requestDispatcher. forward (request, response); 67} 68 else {69 // jump to 70 RequestDispatcher requestDispatcher = request. getRequestDispatcher (error); 71 requestDispatcher. forward (request, response); 72} 73 74} 75}

Index. jsp

1 <% @ page language = "java" import = "java. util. * "contentType =" text/html; charset = UTF-8 "%> 2 <% 3 String path = request. getContextPath (); 4 String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; 5%> 6 7 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 8 

Implemented the login function

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.