首先申明上面的报错红叉,我也不知道怎么回事.总之能运行.
新建项目时选择java EE6.0,低版本没有servlet3.0.
先看一个基本示例.
Test.java是用来测试无需配置文件,无需静态页面(jsp,html)
直接访问servlet来从服务器上获取信息.
Test.java代码
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 @Override 19 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 20 throws ServletException, IOException { 21 doGet(req, resp); 22 } 23 24 @Override 25 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("<h1>思思博士</h1>"); 30 } 31 }
访问结果:
看懂上面的name和urlpattern参数的,继续往下看.
servletConfigDemo.java代码
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.servlet.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 servletConfigDemo extends HttpServlet{ 28 @Override 29 protected void doPost(HttpServletRequest request, HttpServletResponse response) 30 throws ServletException, IOException { 31 ServletConfig config=getServletConfig(); 32 //1.getInitParameter(name)方法 33 String success=config.getInitParameter("success"); 34 String error=config.getInitParameter("error"); 35 36 System.out.println("success-----"+success); 37 System.out.println("errror------"+error); 38 39 //2.getInitParameterNames方法 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方法 49 ServletContext servletContext=config.getServletContext(); 50 System.out.println("servletContext----"+servletContext); 51 52 //4.getServletName方法 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 //判断 61 if(userId!=null&&"gys".equals(userId)&&passwd!=null&&"gys".equals(passwd)){ 62 HttpSession session=request.getSession(); 63 session.setAttribute("user", userId); 64 //跳转 65 RequestDispatcher requestDispatcher=request.getRequestDispatcher(success); 66 requestDispatcher.forward(request, response); 67 } 68 else { 69 //跳转 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 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>登陆</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 <form action="servletConfigDemo.do" method="post"> 25 用户名<input type="text" value="" name="userId" /><br/> 26 密码:<input type="password" name="passwd" /> 27 <input type="submit" value="提交" /> 28 </form> 29 30 </body> 31 </html>
实现登陆功能了
时间: 2024-11-05 21:38:16