Problems and solutions encountered in Javaweb development (2017.5.18)

1. What should I do if the newly created Java  web project using eclipse does not have a deployment descriptor web.xml file?

Right-click the project name-> Java EE  Tools->Generate Deployment descriptor stub

2. When the project is released, which folder is the .class bytecode file generated after compiling the java source files in the src folder?

When publishing to tomcat (start tomcat in eclipse), after the java file in the src folder is compiled, the .class file will be placed in the classes folder in the WEB-INF folder, which will be automatically generated during compilation.

3. In JavaScript , what is the difference between history.go(-1) and History.back()?

go(-1): Return to the previous page, the content in the original page form will be lost; back(-1): Return to the previous page, the content in the original page form will be retained, generally used for back(-1) many.

window.location.reload() //Refresh

window.history.go(1) //Forward

window.history.go(-1) //back

window.history.forward() //Forward

window.history.back() back + refresh

4. How to solve the problem of garbled Chinese characters displayed in Javaweb project development?

Garbled characters appear on the Jsp front page:

①The main reason is the jsp page encoding problem, which can be solved by adding the following <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> to the page.

External JS (JavaScript file) appears garbled:

① This is a headache, calling external *.js (independent JavaScript), where the js file involves inputting Chinese to the front desk, for example: $("#divNameError").html("Please enter your name");document.getElementById ("theClock").innerHTML=”Hello”; Garbled characters are displayed, but if the js code is directly written in the front-end jsp page, it will be displayed normally. The reason is that the encoding of *.js and *.jsp files is not uniform. You can convert the code through NotePad++.

There are garbled characters when the front-end data is obtained by the servlet background:

①Background servlet addition: request.setCharacterEncoding ( " UTF-8" );

                    response.setCharacterEncoding("UTF-8");

②Get data from the foreground of different encodings and add: String linename = newString(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");

③ There is another kind of method attribute that is not written when submitting data through the form form, resulting in garbled characters being obtained in the background.

The more common method is to directly change the Tomcat server.xml file,


5. How to realize the mutual jump of *.jsp files under WEB-INF?

    We know that WEB-INF has relatively high permissions and does not allow direct public access, which means that we have to create a new servlet form for access. But for those who only need to realize the requirement of jumping pages and do not need data processing in the background, obviously this method is relatively low. Can the page jump be realized without building any background middleware? Actually it is possible.

Configure under the web.xml file:


The way to get the front desk is:


Note: <jsp-file> is the path of the actual jump page, and the file must exist; and the name of <url-pattern> has no special restrictions and can be chosen as long as it is meaningful . The acquisition of the front desk adopts the form of project name + url name, which is quite special.

This method is not particularly recommended. The best way is to create a common servlet and implement it by passing parameters.

Background common servlet code:


Foreground code:


6. The implementation of the input box to obtain the focus shadow effect? (border effect)

   

The box-shadow property adds one or more shadows to the box. The basic syntax is {box-shadow:[inset] x-offset y-offset blur-radius spread-radiuscolor} object selector {box-shadow:[ projection method ] X -axis offset Y- axis offset shadow blur radius shadow expand_radius_shadow_color } _

7. The calendar WdatePicker () control is not available when using hyperlinks to jump pages , but it can be used when running this page alone. Why?


       If you put *.jsp outside the WEB-INF directory, you can jump to the page normally by using the hyperlink href , but the page you jump to is not loaded with all externally called js , so it is unavailable. It is recommended that if there is js loading on the jump page, it is best to use the servlet background method to realize the page jump.


8 . How to display time on Jsp page and update in real time synchronously?

 

9 . Realization of Jsp image carousel, and it can be controlled by mouse click. The following code is implemented using jQuery , which is more general.

       <script type="text/javascript">

       $(function(){

         // The mouse slides over the banner , and the left and right buttons are displayed and hidden

         $(".banner").hover(function(){

           $(".lr").show();

         },function(){

           $(".lr").hide();

         });

   // Click the small button below ( the small button is the list li) , and the picture will switch left and right

        $(".anniu li").click(function(){

              //jQuery traverses , siblings() gets the siblings of each element in the matching collection. The removeClass() method removes the selected elements

         $(this).addClass("on").siblings().removeClass("on");

          var num=$(this).index();

          //jQuery animate() method, animation gradient. marginLeft The left margin of the element, slow speed class

         $(".pic").animate({marginLeft:-200*num},"slow");

        });

   // Picture automatic rotation effect

   var a=0;

   varautomatic=setInterval(function(){

            a++;

            a=a%5;

           $(".pic").animate({marginLeft:-200*a},"slow");

            $(".anniuli").eq(a).addClass("on").siblings().removeClass("on");

       },6000);

             // Click the left and right buttons to switch the picture

          $(".pre").click(function(){

            a--;

            a=(a+5)%5;

           $(".pic").animate({marginLeft:-200*a},"slow");

            $(".anniuli").eq(a).addClass("on").siblings().removeClass("on");

          });

          $(".next").click(function(){

            a++;

            a=a%5;

            $(".pic").animate({marginLeft:-200*a},"slow");

            $(".anniuli").eq(a).addClass("on").siblings().removeClass("on");

          });

       });

</script>

     <ulclass="pic">

       <li>

          <ahref="#"><img src="resource/images/ali.png"alt="头像" width="200" height="200"></a>

       </li>

       <li>

          <ahref="#"><img src="resource/images/ali2.png"alt="头像" width="200"height="200"></a>

       </li>

       <li>

          <ahref="#"><img src="resource/images/ali3.png"alt="头像" width="200" height="200"></a>

       </li>

       <li>

          <ahref="#"><img src="resource/images/drem.png"alt="头像" width="200"height="200"></a>

       </li>

       <li>

          <ahref="#"><img src="resource/images/kuer.png"alt="头像" width="200"height="200"></a>

       </li>

     </ul>

     <ulclass="anniu">

       <liclass="on"></li>

       <li></li>

       <li></li>

       <li></li>

       <li></li>

     </ul>

     <ulclass="lr">

       <liclass="pre"><a href="#"> <</a></li>

       <liclass="next"><a href="#"> ></a></li>

     </ul>

 

10. How does the button implement page parameter passing and jumping?

··

 

11. The implementation method of getting the value from the background and judging whether the radio button in the foreground is selected.

 

 

12. How to realize the output percentage in the jsp page?

Considering the simplest code, you can use the method provided by jstl , you need to import <%@ taglib prefix="f"uri="http://java.sun.com/jsp/jstl/fmt"%>

 

13. Let's say the data is inserted into the database successfully. How does the background output prompt information to the foreground, or how does the background call js to the foreground?

 

14. When eclipse imports jQuery-1.8.2.min.js, it always reports an error and displays a red cross, but it does not affect the use. Is there any way to get rid of the red errors?

  Remove the verification of the compiler js: Remove the hook in front of Enable JavaScript semantic validation in Window<Preferences<JavaScript<Validator<Errors/Warnings.

  Delete the .project file in

                    <buildCommand>

                           <name>org.eclipse.wst.jsdt.core.javascriptValidator</name>

                           <arguments>

                           </arguments>

                    </buildCommand>

At this time, the error has not disappeared, you need to delete the project, and then re-import it into the workspace.

 

15. For the submission verification of the front-end form, <input type=”button”> is best to use an ordinary button button to write the submission event; it is not recommended to use submit, mainly because if there is an error in js, it will directly jump out and not execute. After the code, resulting in the submission of js without verification. The specific cases are as follows:



If in the above function, the code jumps out directly due to negligent spelling mistakes and other factors, and submits directly without verification, isn’t this a huge bug? The best way is to change the type to button and submit manually, so that even if the js code makes an error, the form will not be submitted.

 

 

 

 

16. It is required to use cookies to save user login information and display it in the front-end Center.jsp page. The main application and session can't meet the requirements (don't want to frequently pass the reference to the front and back), mainly because the created user login information will disappear when jumping to other pages through the Center, and then returning to the Center page through page redirection. Session is a new session created because the server thinks it is a new form opened, and it is not the same object; and application disappears because Tomcat reloads.

Controller layer cookie creation:


Acquisition of the front-end jsp page:


Note: jquery.cookie.js needs to be introduced for jsp page operations on cookies, otherwise it will not be obtained.

 

17. When using js for form validation, using the document.getElementById("divPwdError").hide(); attribute consistently reports an error, why?

When hide() and show() display and hide divs, jquery-1.8.2.min.js must be imported, because it is a jQuery-specific method, which is not supported by native jsp, and document.getElementById("divPwdError ") can be used .style.visibility=" hidden ";//hidden

document.getElementById("divPwdError").style.visibility="visible"; //显示

or:

document.getElementById("typediv1").style.display="none";//隐藏 

document.getElementById("typediv1").style.display="";//显示 

js realize click button display and hide

functionshowhidediv(id){ 

varsbtitle=document.getElementById(id); 

if(sbtitle){ 

   if(sbtitle.style.display=='block'){ 

   sbtitle.style.display='none'; 

   }else{ 

   sbtitle.style.display='block'; 

   } 

 

18. When js wants to get the value of a certain tag on the jsp page, what should I do if I find that the tag is a div or other tags without a value attribute? Still can't do it?

The answer can be achieved: <divid="divuser" value="<%=cookies[i].getValue()%>">Current user: <%=cookies[i].getValue() %></div>

Js can get var user = $("#divuser").attr("value");

19. The creation of the background controller layer cookie, and the acquisition of the foreground cookie?

Controller layer creates cookie implementation (created to the server):


Jsp front page acquisition (obtained from the server):


Jsp front page acquisition (acquired from the client):


19. How does eclipse import the Apache Tomcat package?

Build Path ->Configure Build Path -> Add Variable -> Server Runtime


 

 

JavaSpring MVC framework development

1. Convert String to map?

Confirm whether your string is in json format. If it is in json format, you can use Gson.jar or json-lib-xx-jdk.jar to automatically parse and parse it.

  It is easier to use Gson, you only need to import a package, but if it uses Object to parse to int type or automatically converts to double type, you need to define an accurate class to parse, and you cannot use Object directly. Example:

 

 

Gson gson = new Gson();

Map<String, Object> map = new HashMap<String,Object>();

map = gson.fromJson(str, map.getClass());

  If you use the json-lib package, you need to import more packages, and you need to import additional commons-lang.jar, ezmorph-1.0.4.jar, commons-logging-1.1.1.jar, commons-collections.jar, commons-beanutils. jar these 5 packages. The parsing example is as follows:

JSONObject jb = JSONObject.fromObject(str);

Map<String, Object> map = (Map<String,Object>)jb;

  If your string is not in json format, then you need to use split to split the string yourself, for example:


2. Click the button on the *.jsp page, the input box will pop up, and the input parameters will be passed to the background Controller layer for execution. How to achieve this?

 

3. In the jsp page, the button button implements the delete function, which requires: ① Pop up a confirmation box; ② Execute the operation of the background Controller layer and pass in parameters; ③ It is required to be universal, and it can also be executed in the foreach statement, and the parameters are based on the clicked position Make changes. ④ There is no hidden domain, which affects security.


4. When using the spring MVC framework for development, page jumps are most likely to cause path problems, which can be solved by mastering the following methods.

<%=request.getContextPath()%> is to solve the relative path problem and return the root path of the site. request.getContextPath() should get the name of the project. If the project is the root directory, you will get a "", which is an empty string. If the project is abc, <%=request.getContextPath()% > will get abc, and the server-side path will be automatically added, <a href="XXXX.jsp"> refers to the xxx.jsp page under the current path, Sometimes you can also set html:base in the head to solve the path problem, but the most used one is request.getContextPath. Get the value of request.getContextPath() in the js file, do not want to write too much JavaScript code in JSP: one way is to use hidden: <input type=hidden name=contextPath value=<%=request.getContextPath()> Furthermore, Scriptlet can also be used to assign values ​​in the js of this page: var a = '<%=request.getContextPath()>'

Refer to the interface in the servlet:

request.getScheme(); The protocol name returned, the default is http

request.getServerName() returns the host name displayed in your browser

getServerPort() Gets the server port number

In practical applications, it is generally used to solve the problem of different paths between jsp testing and production environments:

<%

 String appContext= request.getContextPath();

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

%>

 

 

 

 

 

2. What is the meaning of each annotation in java?

 

 

 

 

 

 

Operation of Oracle data

1. The connection created by the oracle database can be configured under what folder on the client side?

It can be configured under the client D:\ProgramFiles\PLSQL Developer\instantclient_11_2\tnsnames.ora, or it can be configured to connect to an external database.

orcl =

  (DESCRIPTION =

    (ADDRESS_LIST =

      (ADDRESS = (PROTOCOL = TCP)(HOST =127.0.0.1 )(PORT = 1521))

    )

    (CONNECT_DATA =

      (SERVICE_NAME = orcl )

    )

  )

2.

 

 

 

// Attachment: Commonly used regular expressions:

//    Match specific numbers:

// ^[1-9]d*$     // Match positive integers

// ^-[1-9]d*$   // Match negative integers

// ^-?[1-9]d*$   // match integer

// ^[1-9]d*|0$   // Match non-negative integers (positive integers + 0)

// ^-[1-9]d*|0$   // Match non-positive integers (negative integers + 0)

// ^[1-9]d*.d*|0.d*[1-9]d*$    // Match positive floating point numbers

// ^-([1-9]d*.d*|0.d*[1-9]d*)$   // Match negative floating point numbers

// ^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$   // Match floating point numbers

// ^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$    // Match non-negative floating-point numbers (positive floating-point numbers + 0)

// ^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$   // Match non-positive floating-point numbers (negative floating-point numbers +0)

//    Comment: Useful when dealing with large amounts of data, pay attention to corrections in specific applications

//

//    Match a specific string:

// ^[A-Za-z]+$   // Matches a string consisting of 26 English letters

// ^[AZ]+$   // Matches a string consisting of 26 uppercase English letters

// ^[az]+$   // Matches a string consisting of 26 lowercase English letters

// ^[A-Za-z0-9]+$   // Matches a string consisting of numbers and 26 English letters

// ^w+$   // Matches a string consisting of numbers, 26 English letters or underscores

//

//    The verification function and its verification expression when using RegularExpressionValidator to verify the control are introduced as follows:

//

//    Only numbers can be entered: "^[0-9]*$"

//    Only n digits can be entered: "^d{n}$"

//    Only at least n digits can be entered: "^d{n,}$"

//    Only mn digits can be entered: "^d{m,n}$"

//    Only numbers starting with zero and non-zero can be entered: "^(0|[1-9][0-9]*)$"

//    Only positive real numbers with two decimal places can be entered: "^[0-9]+(.[0-9]{2})?$"

//    Only positive real numbers with 1-3 decimal places can be entered: "^[0-9]+(.[0-9]{1,3})?$"

//    Only non-zero positive integers can be entered: "^+?[1-9][0-9]*$"

//    Only non-zero negative integers can be entered: "^-[1-9][0-9]*$"

//    Only characters with a length of 3 can be entered: "^.{3}$"

//    You can only enter a string consisting of 26 English letters: "^[A-Za-z]+$"

//    You can only enter a string consisting of 26 uppercase English letters: "^[AZ]+$"

//    You can only enter a string consisting of 26 lowercase English letters: "^[az]+$"

//    You can only enter a string consisting of numbers and 26 English letters: "^[A-Za-z0-9]+$"

//    You can only enter a string consisting of numbers, 26 English letters or underscores: "^w+$"

//    Verify user password: "^[a-zA-Z]\\w{5,17}$" The correct format is: start with a letter, the length is between 6-18,

//

//    Can only contain characters, numbers and underscores.

//    Verify whether it contains characters such as ^%&',;=?$" : "[^%&',;=?$x22]+"

//    Only Chinese characters can be entered: "^[u4e00-u9fa5],{0,}$"

//    Email location: “^w+[-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$”

//    Verify InternetURL : "^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$"

//    Verify the phone number: "^((d{3,4})|d{3,4}-)?d{7,8}$"

//

//    The correct format is: "XXXX-XXXXXXX" , "XXXX-XXXXXXXX", "XXX-XXXXXXX",

//

//   “XXX-XXXXXXXX”,“XXXXXXX”,“XXXXXXXX”。

//    Verify ID number (15 or 18 digits): "^d{15}|d{}18$"

//    Verify the 12 months of the year : "^(0?[1-9]|1[0-2])$" The correct format is: "01"-"09" and "1" "12"

//    Verify the 31 days of a month: "^((0?[1-9])|((1|2)[0-9])|30|31)$" The correct format is: "01" "09 " and "1" "31".

//

//    Regular expression for matching Chinese characters: [u4e00-u9fa5]

//    Match double-byte characters ( including Chinese characters): [^x00-xff]

//    Regular expression for matching empty lines: n[s| ]*r

//    Regular expression to match HTML tags: /<(.*)>.*|< (.*) />/

//    Regular expression to match leading and trailing spaces: (^s*)|(s*$)

//    Regular expression for matching email addresses: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+) *

//     Regular expression for matching URL : ^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]* )?$

 

Guess you like

Origin blog.csdn.net/youcheng_ge/article/details/77683798