java web入门

package com.servlet;

import java.io.IOException;

import javax.jws.WebService;

import javax.servlet.Servlet;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.annotation.WebServlet;

/*@WebServlet("/demo1")*/
/*@WebServlet(urlPatterns = "/demo1",name = "demo1",
initParams={@WebInitParam(name="user",value="root"),
@webInitParam(name="pwd",value="root")},loadOnStaryup=1)*/

public class ServletDemo1 implements Servlet{

public void destroy() {

System.out.println("这是ServletDemo1的destroy销毁!");

}

public ServletConfig getServletConfig() {

System.out.println("这是ServletDemo1的getServletConfig配置!");

return null;

}

public String getServletInfo() {

System.out.println("这是ServletDemo1的getServletInfo信息!");

return null;

}

public void init(ServletConfig config) throws ServletException {

System.out.println("这是ServletDemo1的init初始化!");

String user = config.getInitParameter("user");

System.out.println(user);

String pwd = config.getInitParameter("pwd");

System.out.println(pwd);

}

public void service(ServletRequest arg0, ServletResponse arg1)

throws ServletException, IOException {

System.out.println("这是ServletDemo1的service服务!");

}

}

用户爱好注册

reg.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>My JSP 'reg.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

<!-- form表单中默认的提交方式为get -->

<form action="demo1" method="post">

用户名:<input type="text" name = "name"><br>

密码:<input type="password" name = "pwd"><br>

爱好:<input type="checkbox" name = "hobbys" value="shopping">shopping

<input type="checkbox" name = "hobbys" value="play">play

<input type="checkbox" name = "hobbys" value="读书">读书<br>

<input type="submit" value="注册"><input type = "reset" value="重置">

</form>

</body>

</html>

Demo1

package com.servlet;

import java.io.IOException;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Set;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@WebServlet("/demo1")

public class Demo1 extends HttpServlet{

/*protected void service(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

form表单中默认的提交方式为get get使用servlet中的编码格式
* 方法1、<form action="demo1" method="get">
* 统一设置编码格式 方法2、 <form action="demo1" method="post">
* 调用request.setCharacterEncoding("UTF-8")*/
//request.getParameter("name")获取form表单中name属性对应名称的value值

/*String name = request.getParameter("name");

System.out.println(name);

String pwd = request.getParameter("pwd");

System.out.println(pwd);

String sex = request.getParameter("sex");

System.out.println(sex);

String hobbys = request.getParameter("hobbys");

System.out.println(hobbys);

String[] hobbys = request.getParameterValues("hobbys");

for (String hobby : hobbys) {

System.out.println(hobby);

}

}*/

//get请求和post请求的区别 post请求地址栏更安全

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

System.out.println("get请求");

String name = request.getParameter("name");

System.out.println(name);

String pwd = request.getParameter("pwd");

System.out.println(pwd);

}@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

System.out.println("post请求");

String name = request.getParameter("name");

System.out.println(name);

String pwd = request.getParameter("pwd");

System.out.println(pwd);

//getParameterMap()获取所有的请求参数,并以Map集合方式返回

Map<String, String[]> map = request.getParameterMap();

Set<Entry<String, String[]>> set = map.entrySet();

for (Entry<String, String[]> entry : set) {

System.out.println(entry.getKey()+":"+entry.getValue());

}

}

}

相关推荐
吾日三省吾码2 小时前
JVM 性能调优
java
y先森3 小时前
CSS3中的伸缩盒模型(弹性盒子、弹性布局)之伸缩容器、伸缩项目、主轴方向、主轴换行方式、复合属性flex-flow
前端·css·css3
前端Hardy3 小时前
纯HTML&CSS实现3D旋转地球
前端·javascript·css·3d·html
susu10830189113 小时前
vue3中父div设置display flex,2个子div重叠
前端·javascript·vue.js
弗拉唐3 小时前
springBoot,mp,ssm整合案例
java·spring boot·mybatis
oi774 小时前
使用itextpdf进行pdf模版填充中文文本时部分字不显示问题
java·服务器
IT女孩儿4 小时前
CSS查缺补漏(补充上一条)
前端·css
少说多做3434 小时前
Android 不同情况下使用 runOnUiThread
android·java
知兀4 小时前
Java的方法、基本和引用数据类型
java·笔记·黑马程序员